Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update syntax for existing type annotations #5298

Merged
merged 2 commits into from Jun 9, 2020

Conversation

pierwill
Copy link
Contributor

@pierwill pierwill commented Jun 5, 2020

Status

Ready for review

Description of Changes

Update syntax for existing type annotations.

Should fix #5282.

@lgtm-com
Copy link

lgtm-com bot commented Jun 5, 2020

This pull request introduces 1 alert when merging b1f28a5 into e300a6e - view on LGTM.com

new alerts:

  • 1 for Syntax error

Uses syntax described in PEP484.
@pierwill
Copy link
Contributor Author

pierwill commented Jun 7, 2020

Not sure what's holding up CI here...

@kushaldas
Copy link
Contributor

After digging more into this I think pylint is not ready yet for type annotations :(

Even after I bring in all the imports into the main code from the if typing.TYPE_CHECKING condition, pylint is still failing.

Related similar issues:

We should discuss about this during the standup tonight.

@pierwill Please hold of any future PR like this until we figure out a way. Otherwise pylint will keep failing them.

@redshiftzero
Copy link
Contributor

It makes sense to migrate away from type comments now that we're on Python 3. To address the lint failures we have the following options:

  1. Not use pylint. Bad because it's a pretty useful tool.
  2. Use pylint but litter the code with noqa exceptions. This is ugly.
  3. Use pylint but add a global exception for E0601 / used-before-assignment which is causing the issue. Bad because it might cause bugs to be missed in the future for that specific lint rule.
  4. Add imports needed for type checking to the imports at runtime and combine them to sidestep the other lint error @kushaldas found. Not a bad option but it means that it's unclear which imports are needed at runtime and which are needed solely for type checking, as well as obviously that there would be unnecessary imports at runtime.
  5. For the imports that are only performed when TYPE_CHECKING, we can express the type hint as a string literal (which mypy will still use).

I think option 5 makes the most sense. We can revert this when pylint-dev/pylint#3285 is addressed upstream. See commit d3707e6 which does this on top of this PR and has a passing lint target. Let me know what you think @kushaldas @pierwill !

@kushaldas
Copy link
Contributor

I think option 5 makes the most sense. We can revert this when pylint-dev/pylint#3285 is addressed upstream. See commit d3707e6 which does this on top of this PR and has a passing lint target. Let me know what you think @kushaldas @pierwill !

Let us go with 5. We will revert back when pylint is ready for type hinting.

@rmol
Copy link
Contributor

rmol commented Jun 9, 2020

Counterpoint: we're not even using pylint on our other projects, just flake8, which evidently doesn't have these problems with type annotations. I think it'd be worth reevaluating whether we really need both, instead of letting one linter's shortcomings dictate a different style of annotation to what we're using everywhere else.

Copy link
Contributor

@kushaldas kushaldas left a comment

Choose a reason for hiding this comment

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

@pierwill thank you for the work. Now you can finish off the rest of the changes following @redshiftzero.

@redshiftzero
Copy link
Contributor

Hmm fair, I'll file an issue for considering pylint removal, when testing this branch I did find more linting issues in pylint compared to flake8 (worth investigating what the difference is). However, we'll still get F821 lint failures even with flake8 only due to our use of if TYPE_CHECKING which we don't hit in other repos because we do the type-checking imports also at runtime, i.e. we'd still need to use the string literal type annotations. Let me know if you think I'm misunderstanding something.

@redshiftzero redshiftzero mentioned this pull request Jun 9, 2020
@kushaldas kushaldas merged commit 2c65008 into freedomofpress:develop Jun 9, 2020
@pierwill pierwill deleted the pierwill-type-syntax-2 branch June 9, 2020 17:06
@rmol rmol added this to the 1.5.0 milestone Jul 15, 2020
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.

Use PEP484 syntax for existing type annotations
4 participants