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 TypedDict.__annotations__ #7126

Open
Pierre-Sassoulas opened this issue Jul 4, 2022 · 7 comments · May be fixed by pylint-dev/astroid#2431
Open

False positive no-member for TypedDict.__annotations__ #7126

Pierre-Sassoulas opened this issue Jul 4, 2022 · 7 comments · May be fixed by pylint-dev/astroid#2431
Assignees
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Help wanted 🙏 Outside help would be appreciated, good for new contributors Needs investigation 🔬 A bug or crash where it's not immediately obvious what is happenning Needs PR This issue is accepted, sufficiently specified and now needs an implementation typing

Comments

@Pierre-Sassoulas
Copy link
Member

Bug description

from typing import TypedDict


class Example(TypedDict):
    string: str
    integer: int
    boolean: bool


print(Example.__annotations__)

See #3234 (comment)

Configuration

No response

Command used

pylint a.py

Pylint output

a.py:10:6: E1101: Class 'Example' has no '__annotations__' member (no-member)

Expected behavior

No warning

Pylint version

2.15.0-dev0

OS / Environment

No response

Additional dependencies

No response

@Pierre-Sassoulas Pierre-Sassoulas added 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 labels Jul 4, 2022
@cdce8p cdce8p added the typing label Aug 15, 2022
@the-real-tokai
Copy link

the-real-tokai commented Nov 25, 2022

I often use a construct like this in Python 3.7 and I get that false positive too:

class foobar():
	""" … """
	
	foo: str
	bar: int
	
	__slots__ = tuple(__annotations__)  # __slots__ "on the fly"
	
	def __init__(self, foo, bar):
		self.foo = foo
		self.bar = bar

	
test = foobar('foobar', 12345)
$ pylint --version
pylint 2.15.6
astroid 2.12.13
Python 3.7.6 (v3.7.6:43364a7ae0, Dec 18 2019, 14:18:50) 
[Clang 6.0 (clang-600.0.57)]

$ pylint thebug1.py 
************* Module thebug1
thebug1.py:9:19: E0602: Undefined variable '__annotations__' (undefined-variable)

------------------------------------------------------------------
Your code has been rated at 3.75/10 (previous run: 3.75/10, +0.00)

@jonathanberthias
Copy link

The issue with __annotations__ seems to have been fixed in the latest release of Pylint:

pylint 3.0.2
astroid 3.0.1
Python 3.11.6 (main, Oct  2 2023, 13:45:54) [GCC 11.4.0]

However, I don't know it it is the same cause, but I am getting no-member errors on TypedDict fields __required_keys__ and __optional_keys__:

from typing import TypedDict


class Example(TypedDict):
    string: str
    integer: int
    boolean: bool


print(Example.__annotations__)  # {'string': <class 'str'>, 'integer': <class 'int'>, 'boolean': <class 'bool'>}
print(Example.__required_keys__)  # frozenset({'integer', 'string', 'boolean'})
print(Example.__optional_keys__)  # frozenset()
no-member.py:11:6: E1101: Class 'Example' has no '__required_keys__' member (no-member)
no-member.py:12:6: E1101: Class 'Example' has no '__optional_keys__' member (no-member)

@Pierre-Sassoulas Pierre-Sassoulas added the Help wanted 🙏 Outside help would be appreciated, good for new contributors label Nov 19, 2023
@Pierre-Sassoulas
Copy link
Member Author

The issue with annotations seems to have been fixed in the latest release of Pylint:

I added the help wanted label because checking that the bug is fixed by adding a functional test case can be done without pylint knowledge (the CI will check from python 3.8 to python 3.12 on win/mac/linux).

However, I don't know it it is the same cause, but I am getting no-member errors on TypedDict fields required_keys and optional_keys:

This require some investigation (And maybe another issue).

@Pierre-Sassoulas Pierre-Sassoulas added the Needs investigation 🔬 A bug or crash where it's not immediately obvious what is happenning label Nov 19, 2023
@robertschweizer
Copy link
Contributor

For me the issue is also fixed for TypedDicts defined with the class-based syntax.

It is still there when using the Alternative Syntax though. We use these for TypedDicts whose fields are not valid attribute names in Python, e.g. because they are reserved keywords.

@Vulcagon
Copy link

Vulcagon commented Apr 2, 2024

I have the same issue and from my tests it also only happens when using the alternative syntax

from typing import TypedDict


class MyTypedDict(TypedDict):
    a: int
    b: str


OtherTypedDict = TypedDict('OtherTypedDict', {'a': int, 'b': str})


print(MyTypedDict.__annotations__)  # No Error
print(OtherTypedDict.__annotations__)  # Error

Instance of TypedDict has no __annotations__ member
E1101:no-member

@mbyrnepr2 mbyrnepr2 self-assigned this May 9, 2024
mbyrnepr2 added a commit to mbyrnepr2/astroid that referenced this issue May 12, 2024
Pylint now does not emit a ``no-member`` error when accessing
``__annotations`` in the following cases:

```
class Test:
    print(__annotations__)
```

```
from typing import TypedDict

OtherTypedDict = TypedDict('OtherTypedDict', {'a': int, 'b': str})
print(OtherTypedDict.__annotations__)
```

Closes pylint-dev/pylint#7126
@dnpetrov
Copy link

The issue with __annotations__ seems to have been fixed in the latest release of Pylint:

pylint 3.0.2
astroid 3.0.1
Python 3.11.6 (main, Oct  2 2023, 13:45:54) [GCC 11.4.0]

Is not fixed in pylint 3.0.3:

$ pylint --version
pylint 3.0.3
astroid 3.0.3
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]

$ cat tmp/test.py 
class Test:
    a: int
    b: float
    c: str

    __slots__ = tuple(__annotations__)

$ pylint tmp/test.py 
************* Module test
tmp/test.py:6:22: E0602: Undefined variable '__annotations__' (undefined-variable)

------------------------------------------------------------------
Your code has been rated at 0.00/10 (previous run: 0.00/10, +0.00)

@mbyrnepr2
Copy link
Member

That’s a slightly different case. The thing that was said to be fixed was not about referencing the attribute in the class body itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Help wanted 🙏 Outside help would be appreciated, good for new contributors Needs investigation 🔬 A bug or crash where it's not immediately obvious what is happenning Needs PR This issue is accepted, sufficiently specified and now needs an implementation typing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants