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

Bug 905 #913

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
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 Feb 27, 2021
8ebc1e0
Instead of creating a new class (by the mean of TYPING_TYPE_TEMPLATE)…
hippo91 Feb 27, 2021
feaddca
Adds doc and type hints
hippo91 Feb 27, 2021
a53ee8d
Adds a unit test for inferring _alias function of the typing module
hippo91 Feb 27, 2021
63c3d96
Formatting according to black
hippo91 Feb 27, 2021
4fbfd10
Adds an entry
hippo91 Feb 27, 2021
b9a649f
Removes useless import
hippo91 Feb 27, 2021
6173010
Update pre-commit config
cdce8p Feb 22, 2021
b20cc9c
Update black version - tox
cdce8p Feb 22, 2021
04b0481
Fix end of files
cdce8p Feb 22, 2021
fe8587b
Fix trailing whitespaces
cdce8p Feb 22, 2021
4037368
Fix black issues
cdce8p Feb 22, 2021
b71974c
Adds inference support for all typing types that are defined through …
hippo91 Feb 27, 2021
ce5f01c
Instead of creating a new class (by the mean of TYPING_TYPE_TEMPLATE)…
hippo91 Feb 27, 2021
bf08bc1
Adds doc and type hints
hippo91 Feb 27, 2021
af0c14c
Adds a unit test for inferring _alias function of the typing module
hippo91 Feb 27, 2021
5fcb911
Formatting according to black
hippo91 Feb 27, 2021
a580ce5
Adds an entry
hippo91 Feb 27, 2021
e556215
Removes useless import
hippo91 Feb 27, 2021
b9c86d3
Takes into account @Pierre-Sassoulas remarks
hippo91 Feb 28, 2021
20a7b65
Enable _alias mocking and testing only if python version is at least 3.7
hippo91 Feb 28, 2021
3dcb5fb
Reformat (black)
hippo91 Feb 28, 2021
9694507
Merge branch 'bug_905' of https://github.com/hippo91/astroid into bug…
hippo91 Feb 28, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# These are supported funding model platforms

tidelift: "pypi/astroid"

1 change: 0 additions & 1 deletion .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@


### ``python -c "from astroid import __pkginfo__; print(__pkginfo__.version)"`` output

2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ astroid.egg-info/
.cache/
.eggs/
.pytest_cache/
.mypy_cache/
.mypy_cache/
13 changes: 7 additions & 6 deletions .pre-commit-config.yaml
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
20 changes: 9 additions & 11 deletions COPYING.LESSER
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ modified by someone else and passed on, the recipients should know
that what they have is not the original version, so that the original
author's reputation will not be affected by problems that might be
introduced by others.

Finally, software patents pose a constant threat to the existence of
any free program. We wish to make sure that a company cannot
effectively restrict the users of a free program by obtaining a
Expand Down Expand Up @@ -113,7 +113,7 @@ modification follow. Pay close attention to the difference between a
"work based on the library" and a "work that uses the library". The
former contains code derived from the library, whereas the latter must
be combined with the library in order to run.

GNU LESSER GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

Expand Down Expand Up @@ -160,7 +160,7 @@ Library.
You may charge a fee for the physical act of transferring a copy,
and you may at your option offer warranty protection in exchange for a
fee.

2. You may modify your copy or copies of the Library or any portion
of it, thus forming a work based on the Library, and copy and
distribute such modifications or work under the terms of Section 1
Expand Down Expand Up @@ -218,7 +218,7 @@ instead of to this License. (If a newer version than version 2 of the
ordinary GNU General Public License has appeared, then you can specify
that version instead if you wish.) Do not make any other change in
these notices.

Once this change is made in a given copy, it is irreversible for
that copy, so the ordinary GNU General Public License applies to all
subsequent copies and derivative works made from that copy.
Expand Down Expand Up @@ -269,7 +269,7 @@ Library will still fall under Section 6.)
distribute the object code for the work under the terms of Section 6.
Any executables containing that work also fall under Section 6,
whether or not they are linked directly with the Library itself.

6. As an exception to the Sections above, you may also combine or
link a "work that uses the Library" with the Library to produce a
work containing portions of the Library, and distribute that work
Expand Down Expand Up @@ -331,7 +331,7 @@ restrictions of other proprietary libraries that do not normally
accompany the operating system. Such a contradiction means you cannot
use both them and the Library together in an executable that you
distribute.

7. You may place library facilities that are a work based on the
Library side-by-side in a single library together with other library
facilities not covered by this License, and distribute such a combined
Expand Down Expand Up @@ -372,7 +372,7 @@ subject to these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties with
this License.

11. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
Expand Down Expand Up @@ -425,7 +425,7 @@ conditions either of that version or of any later version published by
the Free Software Foundation. If the Library does not specify a
license version number, you may choose any version ever published by
the Free Software Foundation.

14. If you wish to incorporate parts of the Library into other free
programs whose distribution conditions are incompatible with these,
write to the author to ask for permission. For software which is
Expand Down Expand Up @@ -459,7 +459,7 @@ SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGES.

END OF TERMS AND CONDITIONS

How to Apply These Terms to Your New Libraries

If you develop a new library, and you want it to be of the greatest
Expand Down Expand Up @@ -506,5 +506,3 @@ if necessary. Here is a sample; alter the names:
Ty Coon, President of Vice

That's all there is to it!


11 changes: 10 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@ What's New in astroid 2.5.1?
============================
Release Date: TBA

* The ``context.path`` is reverted to a set because otherwise it leds to false positives
* The ``context.path`` is reverted to a set because otherwise it leds to false positives
for non `numpy` functions.

Closes #895 #899

* Fix the `Duplicates found in MROs` false positive.

Closes #905
Closes PyCQA/pylint#2717
Closes PyCQA/pylint#3247
Closes PyCQA/pylint#4093
Closes PyCQA/pylint#4131
Closes PyCQA/pylint#4145

What's New in astroid 2.5?
============================
Release Date: 2021-02-15
Expand Down
4 changes: 2 additions & 2 deletions astroid/as_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def _precedence_parens(self, node, child, is_left=True):

def _should_wrap(self, node, child, is_left):
"""Wrap child if:
- it has lower precedence
- same precedence with position opposite to associativity direction
- it has lower precedence
- same precedence with position opposite to associativity direction
"""
node_precedence = node.op_precedence()
child_precedence = child.op_precedence()
Expand Down
4 changes: 2 additions & 2 deletions astroid/brain/brain_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
Guido Van Rossum proposed a hack to handle this in the interpreter:
https://github.com/python/cpython/blob/master/Objects/abstract.c#L186-L189

This brain follows the same logic. It is no wise to add permanently the __class_getitem__ method
This brain follows the same logic. It is no wise to add permanently the __class_getitem__ method
to the type object. Instead we choose to add it only in the case of a subscript node
which inside name node is type.
which inside name node is type.
Doing this type[int] is allowed whereas str[int] is not.

Thanks to Lukasz Langa for fruitful discussion.
Expand Down
52 changes: 52 additions & 0 deletions astroid/brain/brain_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down Expand Up @@ -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
"""

Copy link
Member

@cdce8p cdce8p Feb 27, 2021

Choose a reason for hiding this comment

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

Suggested change
ABC_METACLASS_TEMPLATE = """
from abc import ABCMeta
ABCMeta
"""


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
Copy link
Member

@cdce8p cdce8p Feb 27, 2021

Choose a reason for hiding this comment

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

Suggested change
if res.metaclass():
res.metaclass().locals["__getitem__"] = [func_to_add]
if res != astroid.Uninferable:
if not res.metaclass():
res._metaclass = extract_node(ABC_METACLASS_TEMPLATE)
res.metaclass().locals["__getitem__"] = [func_to_add]
else:
return

return res


MANAGER.register_transform(
nodes.Call,
inference_tip(infer_typing_typevar_or_newtype),
Expand All @@ -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)
2 changes: 1 addition & 1 deletion astroid/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def cached(func, instance, args, kwargs):


class cachedproperty:
""" Provides a cached property equivalent to the stacking of
"""Provides a cached property equivalent to the stacking of
@cached and @property, but more efficient.

After first usage, the <property_name> becomes part of the object's
Expand Down
2 changes: 1 addition & 1 deletion astroid/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def _infer_map(node, context):


def _higher_function_scope(node):
""" Search for the first function which encloses the given
"""Search for the first function which encloses the given
scope. This can be used for looking up in that function's
scope, in case looking up in a lower scope for a particular
name fails.
Expand Down
2 changes: 1 addition & 1 deletion astroid/interpreter/_import/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def _cached_set_diff(left, right):

def _precache_zipimporters(path=None):
"""
For each path that has not been already cached
For each path that has not been already cached
in the sys.path_importer_cache, create a new zipimporter
instance and add it into the cache.
Return a dict associating all paths, stored in the cache, to corresponding
Expand Down
2 changes: 1 addition & 1 deletion astroid/interpreter/objectmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ def attr_fset(self):

def find_setter(func: objects.Property) -> Optional[astroid.FunctionDef]:
"""
Given a property, find the corresponding setter function and returns it.
Given a property, find the corresponding setter function and returns it.

:param func: property for which the setter has to be found
:return: the setter function or None
Expand Down
3 changes: 1 addition & 2 deletions astroid/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ def _infer_name(self, frame, name):
return name

def do_import_module(self, modname=None):
"""return the ast for a module whose name is <modname> imported by <self>
"""
"""return the ast for a module whose name is <modname> imported by <self>"""
# handle special case where we are on a package node importing a module
# using the same name as the package, which may end in an infinite loop
# on relative imports
Expand Down
2 changes: 1 addition & 1 deletion astroid/node_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def _container_getitem(instance, elts, index, context=None):


class NodeNG:
""" A node of the new Abstract Syntax Tree (AST).
"""A node of the new Abstract Syntax Tree (AST).

This is the base class for all Astroid node classes.
"""
Expand Down
2 changes: 1 addition & 1 deletion astroid/raw_building.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def inspect_build(self, module, modname=None, path=None):

def object_build(self, node, obj):
"""recursive method which create a partial ast from real objects
(only function, class, and method are handled)
(only function, class, and method are handled)
"""
if obj in self._done:
return self._done[obj]
Expand Down
6 changes: 3 additions & 3 deletions astroid/scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def builtin_lookup(name):

# TODO move this Mixin to mixins.py; problem: 'FunctionDef' in _scope_lookup
class LocalsDictNodeNG(node_classes.LookupMixIn, node_classes.NodeNG):
""" this class provides locals handling common to Module, FunctionDef
"""this class provides locals handling common to Module, FunctionDef
and ClassDef nodes, including a dict like interface for direct access
to locals information
"""
Expand Down Expand Up @@ -1454,7 +1454,7 @@ def extra_decorators(self):
# pylint: disable=invalid-overridden-method
@decorators_mod.cachedproperty
def type(
self
self,
): # pylint: disable=invalid-overridden-method,too-many-return-statements
"""The function type for this node.

Expand Down Expand Up @@ -1791,7 +1791,7 @@ def _rec_get_names(args, names=None):


def _is_metaclass(klass, seen=None):
""" Return if the given class can be
"""Return if the given class can be
used as a metaclass.
"""
if klass.name == "type":
Expand Down
2 changes: 1 addition & 1 deletion astroid/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


def require_version(minver=None, maxver=None):
""" Compare version of python interpreter to the given one. Skip the test
"""Compare version of python interpreter to the given one. Skip the test
if older.
"""

Expand Down
2 changes: 0 additions & 2 deletions debian.sid/control
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,3 @@ Description: rebuild a new abstract syntax tree from Python's ast
the AST and building an extended ast. The new node
classes have additional methods and attributes for different usages.
Furthermore, astroid builds partial trees by inspecting living objects.


1 change: 0 additions & 1 deletion debian/changelog
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,3 @@ astroid (1.0.0-1) unstable; urgency=low
* new upstream release, project renamed to astroid instead of logilab-astng

-- Sylvain Thénault <sylvain.thenault@logilab.fr> Mon, 29 Jul 2013 16:32:51 +0200

2 changes: 0 additions & 2 deletions debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,3 @@ Description: rebuild a new abstract syntax tree from Python's ast
AST and building an extended ast. The new node classes
have additional methods and attributes for different usages.
Furthermore, astroid builds partial trees by inspecting living objects.


1 change: 0 additions & 1 deletion doc/api/general.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ General API
------------

.. automodule:: astroid

2 changes: 1 addition & 1 deletion doc/changelog.rst
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.. include:: ../ChangeLog
.. include:: ../ChangeLog
3 changes: 1 addition & 2 deletions doc/release.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ Release Process
https://github.com/PyCQA/astroid

6. Run

$ git clean -fd && find . -name '*.pyc' -delete
$ rm dist/*
$ python setup.py sdist --formats=gztar bdist_wheel
$ twine upload dist/*


to release a new version to PyPI.

2 changes: 1 addition & 1 deletion doc/whatsnew.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ The "Changelog" contains *all* nontrivial changes to astroid for the current ver
.. toctree::
:maxdepth: 2

changelog
changelog
Loading