-
-
Notifications
You must be signed in to change notification settings - Fork 278
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
Bug 905 #913
Bug 905 #913
Changes from 7 commits
a91c3af
8ebc1e0
feaddca
a53ee8d
63c3d96
4fbfd10
b9a649f
6173010
b20cc9c
04b0481
fe8587b
4037368
b71974c
ce5f01c
bf08bc1
af0c14c
5fcb911
a580ce5
e556215
b9c86d3
20a7b65
3dcb5fb
9694507
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -11,9 +11,12 @@ | |||||||||||||||||||||
UseInferenceDefault, | ||||||||||||||||||||||
extract_node, | ||||||||||||||||||||||
inference_tip, | ||||||||||||||||||||||
node_classes, | ||||||||||||||||||||||
nodes, | ||||||||||||||||||||||
context, | ||||||||||||||||||||||
InferenceError, | ||||||||||||||||||||||
) | ||||||||||||||||||||||
import astroid | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
TYPING_NAMEDTUPLE_BASENAMES = {"NamedTuple", "typing.NamedTuple"} | ||||||||||||||||||||||
|
@@ -85,6 +88,48 @@ def infer_typing_attr(node, context=None): | |||||||||||||||||||||
return node.infer(context=context) | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
GET_ITEM_TEMPLATE = """ | ||||||||||||||||||||||
@classmethod | ||||||||||||||||||||||
def __getitem__(cls, value): | ||||||||||||||||||||||
return cls | ||||||||||||||||||||||
""" | ||||||||||||||||||||||
|
||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
def _looks_like_typing_alias(node: nodes.Call) -> bool: | ||||||||||||||||||||||
""" | ||||||||||||||||||||||
Returns True if the node corresponds to a call to _alias function. | ||||||||||||||||||||||
For example : | ||||||||||||||||||||||
|
||||||||||||||||||||||
MutableSet = _alias(collections.abc.MutableSet, T) | ||||||||||||||||||||||
|
||||||||||||||||||||||
:param node: call node | ||||||||||||||||||||||
""" | ||||||||||||||||||||||
if isinstance(node, nodes.Call) and isinstance(node.func, nodes.Name): | ||||||||||||||||||||||
if node.func.name == "_alias" and isinstance(node.args[0], nodes.Attribute): | ||||||||||||||||||||||
return True | ||||||||||||||||||||||
return False | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might make sense to take a look at #908 after that. I updated the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I merged #908, a rebase is probably necessary. There are a lot of pre-commit check that are in pylint and not in astroid, I might update astroid configuration once we release 2.7.2, upgraded black is enough for now. |
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
def infer_typing_alias( | ||||||||||||||||||||||
node: nodes.Call, context: context.InferenceContext = None | ||||||||||||||||||||||
) -> node_classes.NodeNG: | ||||||||||||||||||||||
""" | ||||||||||||||||||||||
Infers the call to _alias function | ||||||||||||||||||||||
|
||||||||||||||||||||||
:param node: call node | ||||||||||||||||||||||
:param context: inference context | ||||||||||||||||||||||
""" | ||||||||||||||||||||||
if not isinstance(node, nodes.Call): | ||||||||||||||||||||||
return | ||||||||||||||||||||||
res = next(node.args[0].infer(context=context)) | ||||||||||||||||||||||
# Needs to mock the __getitem__ class method so that | ||||||||||||||||||||||
# MutableSet[T] is acceptable | ||||||||||||||||||||||
func_to_add = extract_node(GET_ITEM_TEMPLATE) | ||||||||||||||||||||||
if res.metaclass(): | ||||||||||||||||||||||
res.metaclass().locals["__getitem__"] = [func_to_add] | ||||||||||||||||||||||
Comment on lines
+132
to
+133
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
return res | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
MANAGER.register_transform( | ||||||||||||||||||||||
nodes.Call, | ||||||||||||||||||||||
inference_tip(infer_typing_typevar_or_newtype), | ||||||||||||||||||||||
|
@@ -93,3 +138,4 @@ def infer_typing_attr(node, context=None): | |||||||||||||||||||||
MANAGER.register_transform( | ||||||||||||||||||||||
nodes.Subscript, inference_tip(infer_typing_attr), _looks_like_typing_subscript | ||||||||||||||||||||||
) | ||||||||||||||||||||||
MANAGER.register_transform(nodes.Call, infer_typing_alias, _looks_like_typing_alias) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copied from the PR description + sorted