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 Type alias cannot refer to itself indirectly in its definition when using dataclasses, NamedTuple or NewType #4770

Closed
phaker opened this issue Mar 14, 2023 · 3 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@phaker
Copy link

phaker commented Mar 14, 2023

Describe the bug
I'm getting a false positive Type alias ... cannot refer to itself indirectly in its definition error on a type alias whose definition refers to a dataclass (or typing.NamedTuple or Newtype).

The exact same line of code triggered #4596 about a month ago, but that one was fixed in 1.1.294 released about a day after I stumbled into it so I just updated and it went away. Now reappeared after minor changes to the types involved.

Screenshots or Code

from dataclasses import dataclass
from typing import TypeAlias, NamedTuple, NewType


AnAlias: TypeAlias = "int|Y"


def f(a: AnAlias) -> None:
    pass


f(0)


# # WORKS
# class Y:
#     pass


@dataclass
class Y:
    pass


# @dataclass(init=False, repr=False, eq=False, match_args=False)
# class Y:
#    def __init__(self):
#        pass


# Y = NewType('Y', str)


# Y = NamedTuple('Y', [])


# class Y(NamedTuple):
#     pass

Which gives me the error: Type alias "AnAlias" cannot refer to itself indirectly in its definition (reportGeneralTypeIssues).

VS Code extension or command-line
Running 1.1.298, with or without strict. Pyright finds no other problems with the code.

Additional context

  • In the the repro below the alias is a union, but in the original code it was a Callable, neither works.

  • Replacing the dataclass with an equivalent hand-crafted class makes it work, other commented out examples break.

  • The alias needs to be "used" before the class is defined, but there's no need to make a value of the alias type before it's fully defined like the f(0) above. This also triggers it:

    def f() -> Callable[[AnAlias], None]:
        def g(a: AnAlias) -> None:
            pass
        return g
    
    
    h = f()
erictraut pushed a commit that referenced this issue Mar 14, 2023
…pe alias declaration in certain circumstances when that type alias was used in a parameter or return type annotation. This addresses #4770.
@erictraut
Copy link
Collaborator

Thanks for the bug report. This will be addressed in the next release.

@erictraut erictraut added bug Something isn't working addressed in next version Issue is fixed and will appear in next published version labels Mar 14, 2023
@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.299, which I just published. It will also be included in a future release of pylance.

@phaker
Copy link
Author

phaker commented Mar 15, 2023

Great. Updated and this fixes the problem.

In the rat's nest I found it in too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants