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

[red-knot] Audit handling of metaclasses in various contexts, add more tests #14200

Open
AlexWaygood opened this issue Nov 8, 2024 · 2 comments
Labels
help wanted Contributions especially welcome red-knot Multi-file analysis & type inference

Comments

@AlexWaygood
Copy link
Member

AlexWaygood commented Nov 8, 2024

Now that we support metaclasses, we should verify that all of the following work as expected. If they do, we should add tests that assert that they do; if they don't, we have some bugs to fix:

from __future__ import annotations
from typing import Literal


class IntIterator:
    def __next__(self) -> int:
        return 42


class Meta(type):
    def __add__(self, other: Meta) -> int:
        return 42

    def __lt__(self, other: Meta) -> Literal[True]:
        return True

    def __getitem__(self, key: int) -> str:
        return "foo"

    def __iter__(self) -> IntIterator:
        return IntIterator()

    def __enter__(self) -> bytes:
        return b"42"

    def __exit__(self, *args) -> None:
        pass


class A(metaclass=Meta): ...
class B(metaclass=Meta): ...


reveal_type(A + B)  # revealed: int
reveal_type(A < B)  # revealed: Literal[True]
reveal_type(A[42])  # revealed: str


for x in A:
    reveal_type(x)  # revealed: int


with A as var:
    reveal_type(var)  # revealed: bytes

Cc. @charliermarsh, if you're interested!

@AlexWaygood AlexWaygood added red-knot Multi-file analysis & type inference help wanted Contributions especially welcome labels Nov 8, 2024
@charliermarsh charliermarsh self-assigned this Nov 8, 2024
@carljm
Copy link
Contributor

carljm commented Nov 9, 2024

I'm torn here, because we definitely do want this eventually, and it makes a lot of sense for @charliermarsh to follow up on it while the metaclass stuff is still fresh, but on the other hand -- I don't think that understanding these aspects of metaclasses is blocking other type system features, nor do I think it has a lot of technical risk, nor do I think it's necessary to pass much of the conformance suite. So is it a priority to work on right now?

@AlexWaygood
Copy link
Member Author

No, not a priority, agreed! There may well be more important things for Charlie to work on. It felt like it wasn't a huge task, though, and as you say, it felt like it make sense to work on it now while metaclass considerations were fresh in our minds.

No strong opinion at all as to whether we work on it now or just add this issue to the backlog to work on later!

@charliermarsh charliermarsh removed their assignment Nov 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Contributions especially welcome red-knot Multi-file analysis & type inference
Projects
None yet
Development

No branches or pull requests

3 participants