capa icon indicating copy to clipboard operation
capa copied to clipboard

Fix: Issue #2307

Open harshit-wadhwani opened this issue 1 year ago • 2 comments

Checklist

  • [x] No CHANGELOG update needed
  • [x] No new tests needed
  • [x] No documentation update needed

harshit-wadhwani avatar Oct 04 '24 16:10 harshit-wadhwani

Annotated was introduced in Python 3.9.

harshit-wadhwani avatar Oct 04 '24 17:10 harshit-wadhwani

we'll remove py3.8 in the next couple days and then we can retry the CI runs. thanks @harshit-wadhwani !

williballenthin avatar Oct 04 '24 19:10 williballenthin

mypy fails, could you take another look, please, @harshit-wadhwani?

capa/render/vverbose.py:202: error: Incompatible types in assignment (expression has type "str", variable has type "Literal['os', 'arch', 'format', 'match', 'characteristic', 'export', 'import', 'section', 'function name', 'substring', 'regex', 'string', 'class', 'namespace', 'api', 'property', 'number', 'bytes', 'offset', 'mnemonic', 'operand number', 'operand offset', 'basic block']")  [assignment]
capa/ida/plugin/model.py:545: error: Incompatible types in assignment (expression has type "str", variable has type "Literal['os', 'arch', 'format', 'match', 'characteristic', 'export', 'import', 'section', 'function name', 'substring', 'regex', 'string', 'class', 'namespace', 'api', 'property', 'number', 'bytes', 'offset', 'mnemonic', 'operand number', 'operand offset', 'basic block']")  [assignment]
capa/ida/plugin/model.py:547: error: Incompatible types in assignment (expression has type "str", variable has type "Literal['os', 'arch', 'format', 'match', 'characteristic', 'export', 'import', 'section', 'function name', 'substring', 'regex', 'string', 'class', 'namespace', 'api', 'property', 'number', 'bytes', 'offset', 'mnemonic', 'operand number', 'operand offset', 'basic block']")  [assignment]
capa/ida/plugin/model.py:549: error: Incompatible types in assignment (expression has type "str", variable has type "Literal['os', 'arch', 'format', 'match', 'characteristic', 'export', 'import', 'section', 'function name', 'substring', 'regex', 'string', 'class', 'namespace', 'api', 'property', 'number', 'bytes', 'offset', 'mnemonic', 'operand number', 'operand offset', 'basic block']")  [assignment]
Found 4 errors in 2 files (checked [20](https://github.com/mandiant/capa/actions/runs/11463966323/job/31899069300?pr=2439#step:9:21)9 source files)

mr-tz avatar Oct 23 '24 07:10 mr-tz

those mypy errors are outside of the code that @harshit-wadhwani has touched, though i agree it would be great if they can fix the errors.

it seems to be due to stricter checking that mypy can now do: mypy now knows that only some strings can be put into the type field, rather than any string, so it complains when a string is assigned there. to fix, we should assert that the string that we want to put there is one of the acceptable strings. something like assert type_name in {"os", "arch", ...}. except i'm not sure we have this list yet today, so it may be a new global constant to add and maintain.

to initialize this list, we could use the list of features in capa.rules.SUPPORTED_FEATURES and extract and deduplicate all the feature.name properties. something like:

VALID_FEATURE_NAMES = set()
for features in capa.rules.SUPPORTED_FEATURES.values():
    for feature in features:
        VALID_FEATURE_NAMES.add(feature.name)

...

assert type_name in VALID_FEATURE_NAMES

hm, but this won't satisfy mypy, cause mypy can't look into this list. so instead i think we need to hardcode the list, like:

VALID_FEATURE_NAMES = ("os", "arch", ...)
...
assert type_name in VALID_FEATURE_NAMES

the downside is that we need to maintain this list, keeping it up to date whenever we add a new feature. but i dont think that will be common or require much work.

williballenthin avatar Oct 23 '24 07:10 williballenthin

I will take a look into those errors.

harshit-wadhwani avatar Oct 23 '24 07:10 harshit-wadhwani

@harshit-wadhwani have you had a chance to take a look here, yet?

mr-tz avatar Nov 27 '24 12:11 mr-tz

I ran pre-commit with dev container and it passes everything, but not on the github actions, any suggestions ? @mr-tz @williballenthin

harshit-wadhwani avatar Nov 29 '24 18:11 harshit-wadhwani

black isn't happy, are you using the same version running in CI (black==24.10.0) and the same config (pre-commit run --all-files black)?

mr-tz avatar Dec 02 '24 11:12 mr-tz

@williballenthin could you take a look if this is what you had in mind to close out the issue, please?

we should then squash and merge this PR

mr-tz avatar Dec 04 '24 17:12 mr-tz

Thanks, @harshit-wadhwani!

mr-tz avatar Dec 05 '24 08:12 mr-tz