pyrefly icon indicating copy to clipboard operation
pyrefly copied to clipboard

UnionType should not be assignable to type

Open NathanTempest opened this issue 8 months ago • 2 comments

Describe the Bug

Pyrefly does not distinguish UnionType from type. Both Optional[X] (which is Union[X, None]) and X | None are accepted where type is expected, but neither is an instance of type at runtime.

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN0B5Ygwi50qKAB1006Zhhg6qAMbKYwuAH0mxGAAoGiRsxgBKOgFoAfHQByYmIml0XdSSHfuACqkqoaMAwwlIx0MPi6ygxwSnTKUKhwMXqscAwYanS4ijpmADR06Lj8qMa6YRGU8HCi6ISeHh7ornTEiXCyMugAxHQAKgAW8DB0cAO4AK5QmK3tdIAoBJzoJVijDCG%2BI-Ht8HQp6GkZu9llZtIqahraJvsMpueq6tHXunpplPddvQBCxQNZOSGcBGY0m0zClGoIUWQhEYgkAG1WAwALpKVbIugAHzsDiUVToAFV0LU%2BiYCkV%2BLlpL0IDUDul0Go9LDaojkSiCrlzHS6AAxCTAmmcemHJn6TE4%2BzoGBckw8mICqBC8SPK65FnCNlQJHLFGfC5PLQayW4mWmEB5EBkKpgKCkQgMWhQCi9LykW320YYHAEOJiSBsCZ%2BOF1YUAZRgIwGDAYxDgiAA9ImbQp7YReGxEzB0InMLhlHBE8oAxAgyHaom6GBeEoAG6oaCobCwf3oQPB1ChrJasRweroMgMcboCx14I1MR0AC8bhAAGZCABGABMnnQCOCULgKOkExJ3F4QUwFkwECqUQg45ndAA5GWilVb9JKRYqgBHCbnmAngDWMFICxDSSG9bwAd18dBn3QEAAF8rRUERxz5aAYAoNAsDwIgyDgoA

As seen from the sandbox link, Pyrefly does not error properly

Both Optional[int] and int | None should be rejected where type or type[Any] is expected. They evaluate to UnionType at runtime, not type.

NathanTempest avatar Mar 26 '26 18:03 NathanTempest

Update: Root cause analysis and path forward

After investigating, the fix for this is not straightforward. The core problem is that Pyrefly uses the same internal representation (Type::Type(Type::Union(...))) for two semantically different things:

The expression int | None — at runtime this is a types.UnionType object, NOT an instance of type The annotation type[int | None] — this means "a class that is int or NoneType", which IS an instance of type Additionally, collapse_builtins_type in simplify.rs normalizes type[int] | type[None] → type[int | None], making these cases literally indistinguishable by the time they reach the subset checker.

A naive rejection of Type::Type(Type::Union(...)) in the subset checker causes hundreds of false positives because it also rejects legitimate patterns like:

type[A | B | C] parameters used with issubclass narrowing type[T] | None class attributes type[X | Y] annotations (explicitly permitted by the typing spec) Type alias definitions involving type[T] To fix this properly, Pyrefly needs a richer type representation that differentiates "type form expressions" (like int | None as a value) from "type annotations" (like type[int | None]). For example, a new TypeExpr variant that untypes like Type::Type for annotations but checks against types.UnionType in the subset checker.

NathanTempest avatar Mar 27 '26 19:03 NathanTempest

This issue has someone assigned, but has not had recent activity for more than 2 weeks.

If you are still working on this issue, please add a comment so everyone knows. Otherwise, please unassign yourself and allow someone else to take over.

Thank you for your contributions!

github-actions[bot] avatar Apr 17 '26 00:04 github-actions[bot]