Skip to content

Conversation

@MVrachev
Copy link
Collaborator

@MVrachev MVrachev commented Nov 19, 2021

Fixes #1657
Related to #1582.

Description of the changes being introduced by the pull request:

This pr includes:

  • configure mypy to show error codes
  • fixed type annotations in test_metadata_serialization
  • two additional invalid serialization tests
  • manual fixes for a lot of mypy warnings.

When there were mypy warnings that we are calling non-annotated function
in annotated context, I decided to add annotations instead of ignoring
those warnings.
That's how I end up adding annotations in the whole tests/utils.py
module.

NOTE: Those changes are not done by automatic tool, but instead are manual.

Please verify and check that the pull request fulfills the following
requirements
:

  • The code follows the Code Style Guidelines
  • Tests have been added for the bug fix or new feature
  • Docs have been added for the bug fix or new feature

@coveralls
Copy link

coveralls commented Nov 19, 2021

Pull Request Test Coverage Report for Build 1500766950

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 9 of 9 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+1.0%) to 98.513%

Totals Coverage Status
Change from base Build 1499283100: 1.0%
Covered Lines: 3799
Relevant Lines: 3825

💛 - Coveralls

Copy link
Member

@jku jku left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick comments:

  • Can you leave comments explaining the ignores -- they don't seem entirely obvious at first glance?
  • Are requests and dateutil really not annotated anywhere ? I wonder how this has not been an issue so far but now requires ignores...
  • test_fetcher (and as follow up tuf/requests_fetcher.py) should not need changing as they are part of legacy code

@jku
Copy link
Member

jku commented Nov 22, 2021

  • Can you leave comments explaining the ignores -- they don't seem entirely obvious at first glance?

I mean PR comments, just for review

targets_key.verify_signature(md_obj, CanonicalJSONSerializer())
with self.assertRaises(exceptions.UnsignedMetadataError):
targets_key.verify_signature(md_obj, JSONSerializer())
targets_key.verify_signature(md_obj, JSONSerializer()) # type: ignore[arg-type]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we receive this warning:

tests/test_api.py:194: error: Argument 2 to "verify_signature" of "Key" has incompatible
type "JSONSerializer"; expected "Optional[SignedSerializer]"  [arg-type]

I disabled it because the test wanted to test setting an explicit set serializer and which will raise
an UnsignedMetadataError and that's what happens.
Please note that JSONSerializer inherits the MetadataSerializer and not the SignedSerializer (as for example the CanonicalJSONSerializer does).
If we replace the JSONSerializer with the CanonicalJSONSerializer the test no longer passes
as we do that same thing on line 192.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this a sign of a bug somewhere then (presumably in the annotations since everything seems to work)?

Copy link
Member

@jku jku Nov 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh... you mean the test explicitly wants to use a serializer argument that is of a completely different class... but that just happens to have a method of same name serialize().

That's a weird test but not a bug then

# Test wrong algorithm format (sslib.FormatError)
snapshot_metafile.hashes = {
256: "8f88e2ba48b412c3843e9bb26e1b6f8fc9e98aceb0fbaa97ba37b4c98717d7ab"
256: "8f88e2ba48b412c3843e9bb26e1b6f8fc9e98aceb0fbaa97ba37b4c98717d7ab" # type: ignore[dict-item]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we receive the following mypy warning:

tests/test_api.py:660: error: Dict entry 0 has incompatible type "int": "str";
expected "str": "str"  [dict-item]

This error hints we have annotated the snapshot_metafile.hashes as a Dict[str, str], but we
are using it as Dict[int, str].
This is expected behavior here as we purposely test an invalid hash algorithm.

@MVrachev
Copy link
Collaborator Author

MVrachev commented Nov 22, 2021

Quick comments:

  • Can you leave comments explaining the ignores -- they don't seem entirely obvious at first glance?

I left comments about each of them.

  • Are requests and dateutil really not annotated anywhere ? I wonder how this has not been an issue so far but now requires ignores...

Hmm, I don't remember why I had to add them to the list in setup.cfg. Now when I remove them there aren't any new warnings.
Will remove them.

  • test_fetcher (and as follow up tuf/requests_fetcher.py) should not need changing as they are part of legacy code

I decided to add type annotations for them as I received warnings that I am calling unannotated code in annotated context.
I don't see it as a problem as those changes are small.

@jku
Copy link
Member

jku commented Nov 22, 2021

I decided to add type annotations for them as I received warnings that I am calling unannotated code in annotated context.

I would like to avoid touching any more legacy code at least in this PR than is necessary.

However, I am interested in those warnings -- where do they come from? Why do we end up importing unannotated code that we did not expect to import?

@MVrachev
Copy link
Collaborator Author

I removed all annotations added to tuf/request_fetcher.py and the mypy warnings I received are:

tests/test_fetcher.py:61: error: Call to untyped function "RequestsFetcher" in typed context  [no-untyped-call]
tests/test_fetcher.py:75: error: Call to untyped function "fetch" in typed context  [no-untyped-call]
tests/test_fetcher.py:84: error: Call to untyped function "fetch" in typed context  [no-untyped-call]
tests/test_fetcher.py:92: error: Call to untyped function "fetch" in typed context  [no-untyped-call]
tests/test_fetcher.py:101: error: Call to untyped function "fetch" in typed context  [no-untyped-call]
tests/test_fetcher.py:117: error: Call to untyped function "fetch" in typed context  [no-untyped-call]

The problem is that we are testing the request_fetcher inside a test for the newer implementation.
Am I correct to consider test_fetcher.py as a test file for the new code?

@MVrachev MVrachev force-pushed the address-mypy-warnings branch from 2a83b1d to b599b92 Compare November 22, 2021 16:09
@MVrachev
Copy link
Collaborator Author

MVrachev commented Nov 22, 2021

I updated the pr with:

  • removing ignore_missing_imports from setup.cfg for requests and dateutil
  • removed changes for tuf/request_fetcher.py and tests/test_fetcher.py as they contain code for the legacy code

@MVrachev MVrachev requested a review from jku November 23, 2021 11:16
Copy link
Member

@jku jku left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, the lack of implicit None return value really makes this a big patch...

Good work: I left some comments but I don't think any are really required fixes. Take a look at the comments but I'm fine with merging as is.

By configuring mypy to show error codes when we get a warning by mypy
we will receive an error code as well.
Those error codes are useful when you want to disable specific mypy
warning for a line with:

Signed-off-by: Martin Vrachev <[email protected]>
In test_metadata_serialization.py "test_case_data" is actually a string
when the decorator calls the actual test functions.

Signed-off-by: Martin Vrachev <[email protected]>
Those tests are about missing "keys" and "roles" attributes in
Targets.Delegations.

Signed-off-by: Martin Vrachev <[email protected]>
This commit includes manual fixes for a lot of mypy warnings.
When there were warnings that we are calling non-annotated function
in annotated context I decided to add annotations instead of ignoring
those warnings.
That's how I end up adding annotations in the whole tests/utils.py
module.

Signed-off-by: Martin Vrachev <[email protected]>
@MVrachev MVrachev force-pushed the address-mypy-warnings branch from b599b92 to e2deff3 Compare November 24, 2021 18:51
@MVrachev MVrachev requested a review from jku November 24, 2021 18:52
@jku jku merged commit 600eb86 into theupdateframework:develop Nov 25, 2021
@MVrachev MVrachev deleted the address-mypy-warnings branch November 25, 2021 11:25
@lukpueh lukpueh mentioned this pull request Dec 13, 2021
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.

New implementation test files: resolve linting issues discovered by pylint and mypy

4 participants