Skip to content

Eliminate unnecessary cast() calls via improved type narrowing#454

Draft
Copilot wants to merge 5 commits intomainfrom
copilot/improve-typing-hints
Draft

Eliminate unnecessary cast() calls via improved type narrowing#454
Copilot wants to merge 5 commits intomainfrom
copilot/improve-typing-hints

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 6, 2026

  • Identify all 13 new mypy errors introduced by our branch
  • Fix sigma/conditions.py:242 - list invariance: add type: ignore comment explaining ConditionIdentifier is a subtype
  • Fix sigma/rule/detection.py:150,152 - add explicit list[SigmaType] type annotation to sigma_val variable
  • Fix sigma/backends/test/backend.py - wrap finalize_query_default calls with str() and fix type: ignore comment error codes
  • Run mypy to verify fixes (down to 3 pre-existing errors only)
  • Run tests to verify no regressions (1369 passed)
  • Run parallel_validation (code review ✅, CodeQL ✅)

Copilot AI and others added 2 commits April 5, 2026 22:11
- types.py: Use int() instead of cast(int, ...) for slice indices; improve
  isinstance-based type narrowing in startswith/endswith; remove redundant
  cast after isinstance in replace_placeholders; extract named function
  for snake_case; fix mutable default argument anti-pattern in escape()
- collection.py: Remove cast after isinstance check in filter list
- conditions.py: Remove self-cast in postprocess; remove unnecessary list
  cast in selector postprocess
- backends/test/backend.py: Remove cast from finalize methods
- conversion/base.py: Use list comprehension with isinstance for type
  narrowing; use typed local variables instead of untyped dict for
  expression backup; replace casts with isinstance-narrowed list in
  convert_condition_as_in_expression; add isinstance check in
  convert_condition_field_eq_expansion
- rule/detection.py: Remove casts from to_plain dict merging; use assert
  for item_linking; use str() instead of cast for regex modifier
- processing/pipeline.py: Remove casts for covariant Mapping/dict types
- processing/transformations/values.py: Use isinstance filtering and
  assert for type narrowing
- processing/resolver.py: Remove unused cast import

Agent-Logs-Url: https://github.com/SigmaHQ/pySigma/sessions/9d74a408-80b0-44ee-bd4a-bac479a824f8

Co-authored-by: thomaspatzke <1845601+thomaspatzke@users.noreply.github.com>
Replace int() and str() with assert isinstance() for type narrowing
in types.py, detection.py, and conversion/base.py to avoid changing
runtime behavior while still properly narrowing types.

Agent-Logs-Url: https://github.com/SigmaHQ/pySigma/sessions/9d74a408-80b0-44ee-bd4a-bac479a824f8

Co-authored-by: thomaspatzke <1845601+thomaspatzke@users.noreply.github.com>
…g-hints

# Conflicts:
#	sigma/backends/test/backend.py
#	sigma/conditions.py
#	sigma/processing/pipeline.py
#	sigma/processing/resolver.py
#	sigma/rule/detection.py

Co-authored-by: thomaspatzke <1845601+thomaspatzke@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants