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

dataclasses.make_dataclass should accept Any, not type #11653

Closed
alexei opened this issue Mar 23, 2024 · 3 comments · Fixed by #11657
Closed

dataclasses.make_dataclass should accept Any, not type #11653

alexei opened this issue Mar 23, 2024 · 3 comments · Fixed by #11657
Labels
help wanted An actionable problem of low to medium complexity where a PR would be very welcome stubs: false positive Type checkers report false errors

Comments

@alexei
Copy link
Contributor

alexei commented Mar 23, 2024

It has been reported before about UnionType vs type. The original bug mentioned something like:

def f1(a: type) -> None:
    ...


f1(int | None)

Resulting in:

Argument 1 to "f1" has incompatible type "UnionType"; expected "type"

The issue has been dismissed saying that UnionType was not a type. But sometimes strange things happen (from a developer's perspective).

Defining a data class in a declarative style works fine:

@dataclass
class D1:
    a: int | None

But doing the same thing programatically fails:

D2 = make_dataclass("D2", [("a", int | None)])

Results in:

List item 0 has incompatible type "tuple[str, UnionType]"; expected "str | tuple[str, type] | tuple[str, type, Any]"

Because UnionType is a class, mypy is technically correct, but isn't the situation ridiculous? D1.a is a UnionType and that wasn't a problem before, but with D2 it is.

Defining named tuples works fine either way:

class NT1(NamedTuple):
    a: int | None


NT2 = NamedTuple("NT2", [("a", int | None)])

Fine, if UnionType is not a union type, then what is the way to express a union type?

See https://mypy-play.net/?mypy=latest&python=3.12&gist=92e2f18ed8b463b0d7675c50bb73ee99

@erictraut
Copy link
Contributor

I think this is a bug in the type definition for make_dataclass in the typeshed stubs. Mypy is correct to generate an error here given the current definition.

@AlexWaygood
Copy link
Member

Yes, Eric's right — our typeshed definition says that you need to provide instances of type to make_dataclass, but really anything valid as a type annotation is arguably okay here. That's inexpressible without something like python/mypy#9773 (which will need a PEP), so we should probably just use Any in typeshed for now.

@JelleZijlstra or @hauntsaninja, could you transfer this issue over to typeshed?

@hauntsaninja hauntsaninja transferred this issue from python/mypy Mar 23, 2024
@JelleZijlstra JelleZijlstra changed the title Expected "type", found "UnionType" dataclasses.make_dataclass should accept Any, not type Mar 24, 2024
@srittau srittau added stubs: false positive Type checkers report false errors help wanted An actionable problem of low to medium complexity where a PR would be very welcome labels Mar 24, 2024
@alexei
Copy link
Contributor Author

alexei commented Mar 25, 2024

I opened a PR #11657

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted An actionable problem of low to medium complexity where a PR would be very welcome stubs: false positive Type checkers report false errors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants