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

Better handling of generic aliases #923

Merged
merged 4 commits into from Apr 7, 2021
Merged

Conversation

cdce8p
Copy link
Member

@cdce8p cdce8p commented Mar 19, 2021

Steps

  • For new features or bug fixes, add a ChangeLog entry describing what your PR does.
  • Write a good description on what the PR does.

Description

Followup to #921, see: #921 (comment)
Improve handling of typing aliases by inserting intermediate classes in mro.

Type of Changes

Type
🐛 Bug fix
✨ New feature

Related Issue

Closes: pylint-dev/pylint#4206
Closes: pylint-dev/pylint#4284

astroid/brain/brain_typing.py Outdated Show resolved Hide resolved
@cdce8p cdce8p force-pushed the type-alias branch 8 times, most recently from a4fc868 to 43625ee Compare April 6, 2021 11:49
@cdce8p cdce8p marked this pull request as ready for review April 6, 2021 11:53
@cdce8p
Copy link
Member Author

cdce8p commented Apr 6, 2021

/CC: @hippo91, @Pierre-Sassoulas

As discussed, this is the followup to #921. It addresses almost all test cases defined in pylint-dev/pylint#4239 with the exception that it can't detect the wrong usage of re.Pattern and re.Match in PY37 and 38. See example below. This would require changes to the brain_re.py which is out of scope for this MR. (Probably not worth it to begin with)

import re
re.Pattern[str]       # [unsubscriptable-object]
re.Match[str]         # [unsubscriptable-object]
var: re.Pattern[int]  # [unsubscriptable-object]

@cdce8p cdce8p force-pushed the type-alias branch 2 times, most recently from f109ae7 to d22fb62 Compare April 6, 2021 16:49
@cdce8p
Copy link
Member Author

cdce8p commented Apr 6, 2021

Small update: I've updated the brain for the re module. With that, all test cases are passing now!

@cdce8p
Copy link
Member Author

cdce8p commented Apr 7, 2021

If there are no objections, I would like to merge this by this weekend. After a new astroid release, we would finally be able to close this chapter.

@Pierre-Sassoulas
Copy link
Member

I think @hippo91 is the one that will really understand what he's reviewing :D I did not see anything wrong with the changes myself.

Copy link
Contributor

@hippo91 hippo91 left a comment

Choose a reason for hiding this comment

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

Well done @cdce8p . I am ok with this PR. Just left a bunch of comments. Feel free to address them or not!

astroid/brain/brain_builtin_inference.py Show resolved Hide resolved
astroid/brain/brain_re.py Show resolved Hide resolved
and value.qname() in TYPING_ALIAS
):
# If typing subscript belongs to an alias
# (PY37+) handle it separately.
Copy link
Contributor

Choose a reason for hiding this comment

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

Well done. I do not know how i can have missed that.

# This is an issue in cases where the aliased class implements it,
# but the typing alias isn't subscriptable. E.g. `typing.ByteString` for PY39+
_forbid_class_getitem_access(class_def)
return iter([class_def])
Copy link
Contributor

Choose a reason for hiding this comment

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

I like very much this concise style! 👍
However you delete a lot of comments. It is not a pb for me if you think they where not helpful.

Copy link
Member Author

Choose a reason for hiding this comment

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

I tend to prefer less / only the necessary comments. Most information is contained in the code already. Comments also have the habit of becoming outdated pretty quickly at which point they are more confusion than actually helpful.

@cdce8p
Copy link
Member Author

cdce8p commented Apr 7, 2021

@hippo91 Thanks for the quick review!

@cdce8p cdce8p merged commit 31a731a into pylint-dev:master Apr 7, 2021
@cdce8p cdce8p deleted the type-alias branch April 7, 2021 11:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants