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

False Positive: no-member for ParamSpec under Python 3.12 #9401

Closed
lord-haffi opened this issue Jan 30, 2024 · 8 comments · Fixed by pylint-dev/astroid#2371
Closed

False Positive: no-member for ParamSpec under Python 3.12 #9401

lord-haffi opened this issue Jan 30, 2024 · 8 comments · Fixed by pylint-dev/astroid#2371
Labels
Astroid Related to astroid False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation python 3.12 Regression typing
Milestone

Comments

@lord-haffi
Copy link

lord-haffi commented Jan 30, 2024

Bug description

A piece of code which worked fine for Python 3.11 (pylint v3.0.3 doesn't report anything here). But with the upgrade to 3.12 pylint suddenly doesn't know the members of ParamSpec anymore. I know that the generic typing got reworked in 3.12, maybe I'm doing something wrong. I took a quick look at the typing package in 3.12 and couldn't find any declaration of ParamSpec so it might be more a problem of the Python 3.12 release.

import inspect
from typing import Callable, TypeVar, ParamSpec

T = TypeVar("T")
P = ParamSpec("P")

def bind_arguments(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> inspect.BoundArguments:
    """
    Binds the given arguments to the given function. If the arguments don't match the functions signature, it will
    raise an error.
    """
    bound_arguments = inspect.signature(func).bind(*args, **kwargs)
    bound_arguments.apply_defaults()
    return bound_arguments

Configuration

No response

Command used

pylint a.py

Pylint output

************* Module a
a.py:5:48: E1101: Instance of 'ParamSpec' has no 'args' member (no-member)
a.py:5:66: E1101: Instance of 'ParamSpec' has no 'kwargs' member (no-member)

Expected behavior

No false-positive here. mypy has no complains about this code and the tests work fine under Python 3.12 as well.

Pylint version

pylint 3.0.3
astroid 3.0.1
Python 3.12.0 (tags/v3.12.0:0fb18b0, Oct  2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)]

OS / Environment

Windows 11

Additional dependencies

No response

@lord-haffi lord-haffi added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Jan 30, 2024
@jacobtylerwalls
Copy link
Member

Hi @lord-haffi, would you mind updating the snippet to include imports and assignments of P and T? Thanks!

@jacobtylerwalls jacobtylerwalls added Regression False Positive 🦟 A message is emitted but nothing is wrong with the code python 3.12 Astroid Related to astroid typing Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Jan 31, 2024
@jacobtylerwalls jacobtylerwalls added this to the 3.0.4 milestone Jan 31, 2024
@jacobtylerwalls
Copy link
Member

Thanks for the report!

This diff to astroid should fix it:

diff --git a/astroid/brain/brain_typing.py b/astroid/brain/brain_typing.py
index ea496440..20276b6c 100644
--- a/astroid/brain/brain_typing.py
+++ b/astroid/brain/brain_typing.py
@@ -437,7 +437,13 @@ def _typing_transform():
     class Generic:
         @classmethod
         def __class_getitem__(cls, item):  return cls
-    class ParamSpec: ...
+    class ParamSpec:
+        @property
+        def args(self):
+            return ParamSpecArgs(self)
+        @property
+        def kwargs(self):
+            return ParamSpecKwargs(self)
     class ParamSpecArgs: ...
     class ParamSpecKwargs: ...
     class TypeAlias: ...

I took a quick look at the typing package in 3.12 and couldn't find any declaration of ParamSpec

This is exactly pylint's issue. In 3.12 CPython decided to C-accelerate the typing module, and that hosed a lot of pylint's ability to inspect typing classes. I stubbed out the bare minimum in pylint-dev/astroid@f2120db, but there's a lot missing.

@DanielNoord
Copy link
Collaborator

@jacobtylerwalls Is this the same issue as with collections? I thought we fixed that by making our import resolved accept the frozen modules?

@jacobtylerwalls
Copy link
Member

I don't know what happened with collections. I'd love to know of a better solution for this, and this sounds like it.

@DanielNoord
Copy link
Collaborator

See pylint-dev/astroid#1512 and related PRs. We might be able to come up with a similar fix here.

@lord-haffi
Copy link
Author

would you mind updating the snippet to include imports and assignments of P and T?

Sure! Well, you already figured it out but for the sake of completeness I edited my post.

Thanks!

@jacobtylerwalls
Copy link
Member

@DanielNoord it's actually not as bad as the collections issue.

I looked into the reason that I stubbed out the typing module. It turns out it was really just to pass about a dozen or so cases in astroid that depend on being able to infer the bases of a class using subscript syntax. It doesn't yet work with the new PEP 695 syntax. We should fix it in a minor release when someone volunteers. In the meantime, I think my stopgap is good for a patch release.

I'll open an astroid issue with my finding.

@jacobtylerwalls
Copy link
Member

Opened pylint-dev/astroid#2370 for the real fix. Will prepare a patch with the band-aid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Astroid Related to astroid False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation python 3.12 Regression typing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants