Skip to content

Infer empty list without annotation for __slots__ and module __all__ #19348

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

sterliakov
Copy link
Collaborator

@sterliakov sterliakov commented Jun 26, 2025

Fixes #10870, fixes #10103.

This adds a fake Iterable[str] context when checking the following:

  • __all__ = [] at top level
  • __slots__ = [] at class level (also works for sets but not for dicts)

Additionally, this fixes a bug with __slots__ being mistakenly checked in other contexts (at top level or in function bodies), so e.g. the following is now accepted:

def foo() -> None:
    __slots__ = 1

@sterliakov sterliakov changed the title Infer empty list without annotation in __slots__ Infer empty list without annotation for __slots__ Jun 26, 2025
@sterliakov sterliakov changed the title Infer empty list without annotation for __slots__ Infer empty list without annotation for __slots__ and module __all__ Jun 27, 2025

This comment has been minimized.

@sterliakov sterliakov requested a review from sobolevn June 27, 2025 15:35

This comment has been minimized.

reveal_type(__slots__) # N: Revealed type is "builtins.int"

[case testSlotsEmptyList]
class A:
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to add more types to the test? Like set, dict, tuple?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Will work with set, won't work with dict (because there's second type argument), will work with tuple (for unrelated reasons, though, () has a well-defined type tuple[()], context won't come into play here). I don't want to pin this dict behavior in test as it isn't ideal, we already have cases with empty tuple. Pushed a testcase with set to have Iterable vs Sequence difference captured by test, that's a good idea.

Copy link
Member

Choose a reason for hiding this comment

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

won't work with dict (because there's second type argument

But, __slots__ can be in a dict form:

>>> class A:
...     __slots__ = {'a': 'docs'}
...     
>>> A().a = 1
>>> A().b = 2
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    A().b = 2
    ^^^^^
AttributeError: 'A' object has no attribute 'b' and no __dict__ for setting new attributes

It can also be empty:

>>> class A:
...     __slots__ = {}

Shouldn't we support this case?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, it's fine at runtime, we have tests for that, only empty dict will still produce "need type annotation".

Shouldn't we support this case?

IMO not. First of all, what's the type of __slots__ there? It's dict[str, ???], we do not have enough information to fill in the second argument. It's no worse than asking for type annotations on empty dicts elsewhere, is it? I don't think this use case is popular enough to add more special-casing down the road. However, I don't think it is important to raise "need type annotation" there, so do not want to add a test explicitly for this behavior.

Copy link
Member

Choose a reason for hiding this comment

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

ok, we can leave this for later :)

Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Thank you!

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

Successfully merging this pull request may close these issues.

Mypy complains when [] is used for __slots__ definition Mypy complains about empty __all__ being untyped
2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy