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

ValueError: Invalid decorator name with pylint-pydantic==0.2.2 and scipy==1.11.1 #23

Closed
GianlucaFicarelli opened this issue Jul 20, 2023 · 2 comments

Comments

@GianlucaFicarelli
Copy link

Problem description

Running pylint with pylint-pydantic==0.2.2 and scipy==1.11.1 may cause ValueError: Invalid decorator name.

  • The error doesn't happen with pylint-pydantic==0.2.1 AND scipy==1.11.1
  • The error doesn't happen with pylint-pydantic==0.2.2 AND scipy==1.10.1

Steps to reproduce the problem

Save to example.py:

"""Test module."""
import scipy


def get_cv(intervals):
    """Test function."""
    scipy.stats.variation(intervals)

create a clean virtualenv and install (currently, the most recent versions):

pip install pylint pylint-pydantic==0.2.2 scipy==1.11.1

and run:

pylint --load-plugins pylint_pydantic example.py

Result:

Exception on node <Call l.7 at 0x103d11a80> in file '/private/tmp/pydantic/example.py'
Traceback (most recent call last):
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/pylint/utils/ast_walker.py", line 91, in walk
    callback(astroid)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/pylint/checkers/base/basic_checker.py", line 713, in visit_call
    if utils.is_terminating_func(node):
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/pylint/checkers/utils.py", line 2197, in is_terminating_func
    for inferred in node.func.infer():
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/nodes/node_ng.py", line 171, in infer
    yield from self._infer(context=context, **kwargs)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/decorators.py", line 143, in raise_if_nothing_inferred
    yield next(generator)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/decorators.py", line 112, in wrapped
    for res in _func(node, context, **kwargs):
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/inference.py", line 358, in infer_attribute
    for owner in self.expr.infer(context):
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/nodes/node_ng.py", line 184, in infer
    for i, result in enumerate(self._infer(context=context, **kwargs)):
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/decorators.py", line 143, in raise_if_nothing_inferred
    yield next(generator)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/decorators.py", line 112, in wrapped
    for res in _func(node, context, **kwargs):
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/inference.py", line 372, in infer_attribute
    yield from owner.igetattr(self.attrname, context)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/nodes/scoped_nodes/scoped_nodes.py", line 438, in igetattr
    return bases._infer_stmts(self.getattr(name, context), context, frame=self)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/nodes/scoped_nodes/scoped_nodes.py", line 414, in getattr
    result = [self.import_module(name, relative_only=True)]
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/nodes/scoped_nodes/scoped_nodes.py", line 530, in import_module
    return AstroidManager().ast_from_module_name(
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/manager.py", line 246, in ast_from_module_name
    return self.ast_from_file(found_spec.location, modname, fallback=False)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/manager.py", line 138, in ast_from_file
    return AstroidBuilder(self).file_build(filepath, modname)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/builder.py", line 145, in file_build
    return self._post_build(module, builder, encoding)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/builder.py", line 166, in _post_build
    self.add_from_names_to_locals(from_node)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/builder.py", line 223, in add_from_names_to_locals
    imported = node.do_import_module()
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/nodes/_base_nodes.py", line 146, in do_import_module
    return mymodule.import_module(
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/nodes/scoped_nodes/scoped_nodes.py", line 530, in import_module
    return AstroidManager().ast_from_module_name(
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/manager.py", line 246, in ast_from_module_name
    return self.ast_from_file(found_spec.location, modname, fallback=False)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/manager.py", line 138, in ast_from_file
    return AstroidBuilder(self).file_build(filepath, modname)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/builder.py", line 145, in file_build
    return self._post_build(module, builder, encoding)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/builder.py", line 166, in _post_build
    self.add_from_names_to_locals(from_node)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/builder.py", line 223, in add_from_names_to_locals
    imported = node.do_import_module()
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/nodes/_base_nodes.py", line 146, in do_import_module
    return mymodule.import_module(
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/nodes/scoped_nodes/scoped_nodes.py", line 530, in import_module
    return AstroidManager().ast_from_module_name(
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/manager.py", line 246, in ast_from_module_name
    return self.ast_from_file(found_spec.location, modname, fallback=False)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/manager.py", line 138, in ast_from_file
    return AstroidBuilder(self).file_build(filepath, modname)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/builder.py", line 145, in file_build
    return self._post_build(module, builder, encoding)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/builder.py", line 173, in _post_build
    module = self._manager.visit_transforms(module)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/manager.py", line 109, in visit_transforms
    return self._transform.visit(node)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/transforms.py", line 89, in visit
    return self._visit(module)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/transforms.py", line 54, in _visit
    visited = self._visit_generic(value)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/transforms.py", line 61, in _visit_generic
    return [self._visit_generic(child) for child in node]
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/transforms.py", line 61, in <listcomp>
    return [self._visit_generic(child) for child in node]
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/transforms.py", line 67, in _visit_generic
    return self._visit(node)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/transforms.py", line 54, in _visit
    visited = self._visit_generic(value)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/transforms.py", line 61, in _visit_generic
    return [self._visit_generic(child) for child in node]
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/transforms.py", line 61, in <listcomp>
    return [self._visit_generic(child) for child in node]
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/transforms.py", line 67, in _visit_generic
    return self._visit(node)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/transforms.py", line 57, in _visit
    return self._transform(node)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/astroid/transforms.py", line 39, in _transform
    ret = transform_func(node)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/pylint_pydantic/__init__.py", line 53, in transform
    if _is_classmethod_decorator(node):
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/pylint_pydantic/__init__.py", line 42, in _is_classmethod_decorator
    decorator_name = _get_decorator_name(decorator)
  File "/private/tmp/pydantic/venv2/lib/python3.10/site-packages/pylint_pydantic/__init__.py", line 33, in _get_decorator_name
    raise ValueError("Invalid decorator name")
ValueError: Invalid decorator name
************* Module example
example.py:1:0: F0002: example.py: Fatal error while checking 'example.py'. Please open an issue in our bug tracker so we address this. There is a pre-filled template that you can use in '.......txt'. (astroid-error)

-----------------------------------
Your code has been rated at 0.00/10

Specifications like the version of the project, operating system, or hardware

  • pylint version: 2.17.4
  • pydantic version: 2.0.3
  • python version: 3.10
  • pylint-pydantic version: 0.2.2
  • scipy version: 1.11.1
  • astroid version: 2.15.6
pip freeze
annotated-types==0.5.0
astroid==2.15.6
dill==0.3.6
isort==5.12.0
lazy-object-proxy==1.9.0
mccabe==0.7.0
numpy==1.25.1
platformdirs==3.9.1
pydantic==2.0.3
pydantic_core==2.3.0
pylint==2.17.4
pylint-plugin-utils==0.8.2
pylint-pydantic==0.2.2
scipy==1.11.1
tomli==2.0.1
tomlkit==0.11.8
typing_extensions==4.7.1
wrapt==1.15.0
@GianlucaFicarelli
Copy link
Author

Debugging the error, it seems caused by a decorator being a lambda defined here:
https://github.com/scipy/scipy/blob/defcb37766e99d77e1997728d65a890efe895074/scipy/stats/_continuous_distns.py#L3785
and failing to be identified in

def _get_decorator_name(decorator):
# @validator(pre=True)
if isinstance(decorator, Call):
decorator = decorator.func
# @validator
if isinstance(decorator, Name):
return decorator.name
# @pydantic.validator
if isinstance(decorator, Attribute):
return decorator.attrname
raise ValueError("Invalid decorator name")

@GianlucaFicarelli
Copy link
Author

Thanks @fcfangcc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant