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
It has been reported before about
UnionTypevstype. The original bug mentioned something like:Resulting in:
The issue has been dismissed saying that
UnionTypewas not atype. But sometimes strange things happen (from a developer's perspective).Defining a data class in a declarative style works fine:
But doing the same thing programatically fails:
Results in:
Because
UnionTypeis a class,mypyis technically correct, but isn't the situation ridiculous?D1.ais aUnionTypeand that wasn't a problem before, but withD2it is.Defining named tuples works fine either way:
Fine, if
UnionTypeis 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