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

Introduce flake8-pyi in pre-commit checks #1253

Merged
merged 13 commits into from Nov 13, 2022

Conversation

hoefling
Copy link
Contributor

@hoefling hoefling commented Nov 13, 2022

I have made things!

This PR adds pre-commit check using flake8-pyi. Most of the warnings are fixed alongside, warnings that conflict with the current mypy state (e.g. python/mypy#11098, python/mypy#12393 or python/mypy#13437) are disabled with an explicit comment linking to the related mypy issue.

Warnings that were disabled globally:

  • Y021 - looks like docstrings are used here and there, esp. the one for Field stub is useful
  • Y024 - switching from namedtuple to NamedTuple can be done in an automated fashion, but fields will be typed with Any. Not sure if this will be of any help
  • Y043 - IMO renaming the aliases is more harm than good

Related issues

Closes #128

…follow flag

Signed-off-by: Oleg Hoefling <oleg.hoefling@ionos.com>
Signed-off-by: Oleg Hoefling <oleg.hoefling@ionos.com>
Signed-off-by: Oleg Hoefling <oleg.hoefling@ionos.com>
…t case

Signed-off-by: Oleg Hoefling <oleg.hoefling@ionos.com>
Signed-off-by: Oleg Hoefling <oleg.hoefling@ionos.com>
Signed-off-by: Oleg Hoefling <oleg.hoefling@ionos.com>
Signed-off-by: Oleg Hoefling <oleg.hoefling@ionos.com>
…ypes with collections.abc

Signed-off-by: Oleg Hoefling <oleg.hoefling@ionos.com>
Signed-off-by: Oleg Hoefling <oleg.hoefling@ionos.com>
Signed-off-by: Oleg Hoefling <oleg.hoefling@ionos.com>
Signed-off-by: Oleg Hoefling <oleg.hoefling@ionos.com>
@hoefling hoefling marked this pull request as draft November 13, 2022 16:36
@hoefling hoefling changed the title Pre commit/flake8 pyi Introduce flake8-pyi in pre-commit checks Nov 13, 2022
Signed-off-by: Oleg Hoefling <oleg.hoefling@ionos.com>
@hoefling hoefling marked this pull request as ready for review November 13, 2022 16:52
Signed-off-by: Oleg Hoefling <oleg.hoefling@ionos.com>
Copy link
Contributor

@adamchainz adamchainz left a comment

Choose a reason for hiding this comment

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

Thank you for working on setting this up.

The PR is way too big to review or merge as-is. I would prefer to split this up to activate one or a two rules at a time.

Also I hope you didn’t manually rewrite for changes like PEP 585, pyupgrade can do those automatically. That’s another tool we could add to pre-commit.

Comment on lines 35 to +39
- id: flake8
additional_dependencies:
- flake8-pyi
types: []
files: ^.*.pyi?$
Copy link
Contributor

Choose a reason for hiding this comment

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

This has stopped flake8 from running on the repo’s Python files, so we need two configurations. Also, please pin the version of flake8-pyi so there are no surprise failures when a new version is released.

Suggested change
- id: flake8
additional_dependencies:
- flake8-pyi
types: []
files: ^.*.pyi?$
- id: flake8
- id: flake8
additional_dependencies:
- flake8-pyi==22.10.0
types: []
files: ^.*.pyi?$

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@adamchainz the glob includes .py files (note the trailing question mark), so the flake8 hook will run on both .py and .pyi files. I can introduce a second hook if you want tho 😎

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point, missed that! No need for a second hook, I think, unless flake8-pyi rules will be problematic for plain python files.

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

This looks great! I will merge this asap, so no merge-conflicts on this side will emerge.

We can later work on any further improvements.

Thank you so much, this is amazing! 🎉 👍 😊

@sobolevn sobolevn merged commit b81b1bf into typeddjango:master Nov 13, 2022
@sobolevn
Copy link
Member

The PR is way too big to review or merge as-is. I would prefer to split this up to activate one or a two rules at a time.

Sorry, @adamchainz I've missed this comment :(

@sobolevn
Copy link
Member

I personally like to merge big style changes as-is, because otherwise it is really hard to work with merge conflicts and other PRs.

This becomes a kind of a blocker to the whole process.

@hoefling
Copy link
Contributor Author

hoefling commented Nov 13, 2022

@adamchainz I'm not against splitting the PR in separate ones, but even fixing a single rule per PR will end in a huge diff (esp. the PEP 604/PEP 585 stuff). Do you want me to go with enabling rules per modules in flake8 config? This is surely doable, but will take time, as there's nothing I can automate on a short sight, will have to redo the scripts first.

Edit: too late, @sobolevn merged already 😎

- case: client_follow_flag
main: |
from django.test.client import Client
client = Client()
Copy link
Collaborator

Choose a reason for hiding this comment

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

What's up with these test changes? Did you base this on some other branch that isn't master?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@intgr whoops you are right, my bad 🤦 this also closed #1252. Will prepare a PR that reverts the changes shortly

@hoefling hoefling deleted the pre-commit/flake8-pyi branch November 13, 2022 17:46
@intgr
Copy link
Collaborator

intgr commented Nov 13, 2022

Thanks! I think the pipe syntax is so much easier on the eyes than Union[] and Optional[].

@intgr
Copy link
Collaborator

intgr commented Nov 14, 2022

I notice you simplified float | int to float in many places.

Are we affected by this now? python/typeshed#9130

Since mypy 0.990 type promotions was limited.
This means that complex is not longer promoted to int/float, therefore we should adapt the types to list all possible types

@sobolevn
Copy link
Member

Yes, we are. float | int and bytes | bytearray | memoryview

@intgr
Copy link
Collaborator

intgr commented Nov 14, 2022

Should we revert this part of the PR?

Maybe hoeflint can re-run the script on an earlier commit, with this particular change disabled. And then submit a PR with the diffs.

@sobolevn
Copy link
Member

Yes, we should 👍

@hoefling
Copy link
Contributor Author

Yep, can revert that. IIRC there were only float | int and complex | int cases here and there.

hoefling added a commit to hoefling/django-stubs that referenced this pull request Nov 14, 2022
…41 rule

Signed-off-by: Oleg Hoefling <oleg.hoefling@ionos.com>
sobolevn pushed a commit that referenced this pull request Nov 15, 2022
)

Signed-off-by: Oleg Hoefling <oleg.hoefling@ionos.com>

Signed-off-by: Oleg Hoefling <oleg.hoefling@ionos.com>
intgr pushed a commit to typeddjango/djangorestframework-stubs that referenced this pull request Nov 16, 2022
This is a PR similar to typeddjango/django-stubs#1253, only for rest_framework-stubs.

All statements that are unclear on how to proceed are marked with # noqa: in the code, with the exception of compat.pyi, which has additional rules turned off in setup.cfg.

* configure flake8-pyi plugin in pre-commit
* fix Y015
* fix Y037
* use PEP 585 container types where possible, replace obsolete typing types with collections.abc
* disable Y041 rule
* add typealias annotation where feasible, add self annotations
* fix failing tests, silence remaining warnings since unclear how to proceed
* address review
* amend tests
* amend rebase error

Signed-off-by: Oleg Hoefling <oleg.hoefling@ionos.com>
fget: Callable[[Self], _Get] | None
def __init__(self: Self, method: Callable[[Self], _Get] | None = ...) -> None: ...
def __get__(self: Self, instance: Self | None, cls: type[Self] = ...) -> _Get: ...
def getter(self: Self, method: Callable[[Self], _Get]) -> classproperty[_Get]: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

These self: Self annotations seem to cause regression #1285, I have submitted a PR to fix it in #1287.

intgr added a commit that referenced this pull request Dec 9, 2022
Removed unnecessary `Self` hints on classproperty methods.

The removal of `self: Self` fixes #1285. Regressed in #1253.
brianhelba added a commit to brianhelba/django-stubs that referenced this pull request Aug 30, 2023
typeddjango#1253 switched this project
to use the union type syntax ( https://peps.python.org/pep-0604/ ),
which is only available in Python >=3.10. Earlier versions of Python have no
support for this syntax.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Setup flake8-pyi to lint stubs
4 participants