-
-
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
Closed
Closed
Bug 905 #913
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
a91c3af
Adds inference support for all typing types that are defined through …
hippo91 8ebc1e0
Instead of creating a new class (by the mean of TYPING_TYPE_TEMPLATE)…
hippo91 feaddca
Adds doc and type hints
hippo91 a53ee8d
Adds a unit test for inferring _alias function of the typing module
hippo91 63c3d96
Formatting according to black
hippo91 4fbfd10
Adds an entry
hippo91 b9a649f
Removes useless import
hippo91 6173010
Update pre-commit config
cdce8p b20cc9c
Update black version - tox
cdce8p 04b0481
Fix end of files
cdce8p fe8587b
Fix trailing whitespaces
cdce8p 4037368
Fix black issues
cdce8p b71974c
Adds inference support for all typing types that are defined through …
hippo91 ce5f01c
Instead of creating a new class (by the mean of TYPING_TYPE_TEMPLATE)…
hippo91 bf08bc1
Adds doc and type hints
hippo91 af0c14c
Adds a unit test for inferring _alias function of the typing module
hippo91 5fcb911
Formatting according to black
hippo91 a580ce5
Adds an entry
hippo91 e556215
Removes useless import
hippo91 b9c86d3
Takes into account @Pierre-Sassoulas remarks
hippo91 20a7b65
Enable _alias mocking and testing only if python version is at least 3.7
hippo91 3dcb5fb
Reformat (black)
hippo91 9694507
Merge branch 'bug_905' of https://github.com/hippo91/astroid into bug…
hippo91 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
# These are supported funding model platforms | ||
|
||
tidelift: "pypi/astroid" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,3 @@ | |
|
||
|
||
### ``python -c "from astroid import __pkginfo__; print(__pkginfo__.version)"`` output | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,4 @@ astroid.egg-info/ | |
.cache/ | ||
.eggs/ | ||
.pytest_cache/ | ||
.mypy_cache/ | ||
.mypy_cache/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
repos: | ||
- repo: https://github.com/ambv/black | ||
rev: 18.6b4 | ||
rev: 20.8b1 | ||
hooks: | ||
- id: black | ||
args: [--safe, --quiet] | ||
exclude: tests/testdata | ||
python_version: python3.6 | ||
exclude: tests/testdata|doc/ | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v1.2.3 | ||
rev: v3.4.0 | ||
hooks: | ||
- id: trailing-whitespace | ||
- id: end-of-file-fixer | ||
- id: trailing-whitespace | ||
exclude: .github/|tests/testdata | ||
- id: end-of-file-fixer | ||
exclude: tests/testdata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,17 +4,22 @@ | |||||||||||||||||
# Copyright (c) 2018 Bryce Guinta <bryce.paul.guinta@gmail.com> | ||||||||||||||||||
|
||||||||||||||||||
"""Astroid hooks for typing.py support.""" | ||||||||||||||||||
import sys | ||||||||||||||||||
import typing | ||||||||||||||||||
|
||||||||||||||||||
from astroid import ( | ||||||||||||||||||
MANAGER, | ||||||||||||||||||
UseInferenceDefault, | ||||||||||||||||||
extract_node, | ||||||||||||||||||
inference_tip, | ||||||||||||||||||
node_classes, | ||||||||||||||||||
nodes, | ||||||||||||||||||
context, | ||||||||||||||||||
InferenceError, | ||||||||||||||||||
) | ||||||||||||||||||
import astroid | ||||||||||||||||||
|
||||||||||||||||||
PY37 = sys.version_info[:2] >= (3, 7) | ||||||||||||||||||
|
||||||||||||||||||
TYPING_NAMEDTUPLE_BASENAMES = {"NamedTuple", "typing.NamedTuple"} | ||||||||||||||||||
TYPING_TYPEVARS = {"TypeVar", "NewType"} | ||||||||||||||||||
|
@@ -85,6 +90,50 @@ def infer_typing_attr(node, context=None): | |||||||||||||||||
return node.infer(context=context) | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
GET_ITEM_TEMPLATE = """ | ||||||||||||||||||
@classmethod | ||||||||||||||||||
def __getitem__(cls, value): | ||||||||||||||||||
return cls | ||||||||||||||||||
""" | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
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 | ||||||||||||||||||
""" | ||||||||||||||||||
return ( | ||||||||||||||||||
isinstance(node, nodes.Call) | ||||||||||||||||||
and isinstance(node.func, nodes.Name) | ||||||||||||||||||
and node.func.name == "_alias" | ||||||||||||||||||
and isinstance(node.args[0], nodes.Attribute) | ||||||||||||||||||
) | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
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 +142,6 @@ def infer_typing_attr(node, context=None): | |||||||||||||||||
MANAGER.register_transform( | ||||||||||||||||||
nodes.Subscript, inference_tip(infer_typing_attr), _looks_like_typing_subscript | ||||||||||||||||||
) | ||||||||||||||||||
|
||||||||||||||||||
if PY37: | ||||||||||||||||||
MANAGER.register_transform(nodes.Call, infer_typing_alias, _looks_like_typing_alias) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,3 @@ General API | |
------------ | ||
|
||
.. automodule:: astroid | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
.. include:: ../ChangeLog | ||
.. include:: ../ChangeLog |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.