Skip to content

Conversation

KotlinIsland
Copy link
Contributor

@KotlinIsland KotlinIsland commented Sep 8, 2025

UnionType.__or__/__ror__

from types import UnionType
from typing import _SpecialForm

def f(u: UnionType, s: _SpecialForm):
    x = u | s
    reveal_type(x)  # UnionType, is actually _SpecialForm (is actually _UnionGenericAlias)
f(int | str, Literal[1])

this is rather unfortunate due to NotImplemented semantics, which are underspecified at this time, very relevant:

to work around this we add the known case _SpecialForm to the return type

and

x = int | int | int
reveal_type(x) # UnionType, is actually type[int]

to work around this we add the special case type to the return type, in the case of "unions are invalid as return types", then i would suggest UnionType | Any

UnionType.__getitem__

T = TypeVar("T")
A = int | list[T]

A[int]

it has __getitem__, the design was inspired by _SpecialForm.__getitem__ but suggestions are appreciated

Copy link
Contributor

github-actions bot commented Sep 8, 2025

Diff from mypy_primer, showing the effect of this PR on open source code:

freqtrade (https://github.com/freqtrade/freqtrade)
+ freqtrade/optimize/hyperopt/hyperopt_optimizer.py:405: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]

xarray (https://github.com/pydata/xarray)
+ xarray/namedarray/core.py: note: In member "chunk" of class "NamedArray":
+ xarray/namedarray/core.py:813: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ xarray/core/variable.py: note: In member "_in_memory" of class "Variable":
+ xarray/core/variable.py:435: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ xarray/core/variable.py: note: At top level:
+ xarray/structure/alignment.py: note: In function "deep_align":
+ xarray/structure/alignment.py:993: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ xarray/structure/merge.py: note: In function "merge":
+ xarray/structure/merge.py:1012: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ xarray/core/resample_cftime.py: note: In member "__init__" of class "CFTimeGrouper":
+ xarray/core/resample_cftime.py:105: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ xarray/core/resample_cftime.py: note: In function "_adjust_bin_edges":
+ xarray/core/resample_cftime.py:286: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ xarray/core/dataset.py: note: In member "_validate_indexers" of class "Dataset":
+ xarray/core/dataset.py:2622: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ xarray/core/dataarray.py: note: In member "chunk" of class "DataArray":
+ xarray/core/dataarray.py:1474: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ xarray/core/dataarray.py: note: At top level:
+ xarray/core/dataarray.py: note: In member "_binary_op" of class "DataArray":
+ xarray/core/dataarray.py:4885: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ xarray/tests/test_variable.py: note: In function "test_datetime_conversion":
+ xarray/tests/test_variable.py:3131: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ xarray/tests/test_cftime_offsets.py: note: In function "test_rollforward":
+ xarray/tests/test_cftime_offsets.py:1004: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ xarray/tests/test_cftime_offsets.py:1006: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ xarray/tests/test_cftime_offsets.py: note: In function "test_rollback":
+ xarray/tests/test_cftime_offsets.py:1055: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ xarray/tests/test_cftime_offsets.py:1057: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]

sphinx (https://github.com/sphinx-doc/sphinx)
+ sphinx/pycode/ast.py: note: In member "visit_Constant" of class "_UnparseVisitor":
+ sphinx/pycode/ast.py:137:37: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ sphinx/util/_serialise.py: note: In function "stable_hash":
+ sphinx/util/_serialise.py:22:24: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ sphinx/util/_serialise.py: note: In function "_stable_str_prep":
+ sphinx/util/_serialise.py:45:24: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ sphinx/util/nodes.py: note: In function "apply_source_workaround":
+ sphinx/util/nodes.py:201:13: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ sphinx/util/nodes.py: note: At top level:
+ sphinx/transforms/__init__.py: note: In member "apply" of class "ApplySourceWorkaround":
+ sphinx/transforms/__init__.py:244:33: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ sphinx/config.py: note: In function "is_serializable":
+ sphinx/config.py:68:26: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ sphinx/config.py: note: In member "match" of class "ENUM":
+ sphinx/config.py:92:30: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ sphinx/ext/autodoc/preserve_defaults.py: note: In function "_get_arguments_inner":
+ sphinx/ext/autodoc/preserve_defaults.py:109:22: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ sphinx/writers/texinfo.py: note: In member "visit_title" of class "TexinfoTranslator":
+ sphinx/writers/texinfo.py:641:31: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ sphinx/ext/apidoc/_extension.py: note: In function "_check_collection_of_strings":
+ sphinx/ext/apidoc/_extension.py:246:37: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]

archinstall (https://github.com/archlinux/archinstall)
+ archinstall/lib/general.py:71: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]

core (https://github.com/home-assistant/core)
+ homeassistant/components/tplink/sensor.py:322: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]

attrs (https://github.com/python-attrs/attrs)
+ tests/typing_example.py:257: error: Argument 1 to "instance_of" has incompatible type "UnionType | <typing special form> | type"; expected "type[Never]"  [arg-type]

Tanjun (https://github.com/FasterSpeeding/Tanjun)
+ tanjun/commands/slash.py:378: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ tanjun/commands/slash.py:1642: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ tanjun/commands/message.py:106: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ tanjun/commands/message.py:163: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ tanjun/commands/message.py:234: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ tanjun/commands/menu.py:173: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ tanjun/commands/menu.py:300: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ tanjun/commands/menu.py:520: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]

egglog-python (https://github.com/egraphs-good/egglog-python)
+ python/egglog/egraph_state.py:643: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ python/egglog/runtime.py:247: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]

zulip (https://github.com/zulip/zulip)
+ zerver/lib/validator.py:596: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ zerver/openapi/markdown_extension.py:257: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ zerver/middleware.py:402: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]

artigraph (https://github.com/artigraph/artigraph)
+ src/arti/internal/type_hints.py:146: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ src/arti/internal/type_hints.py:148: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ src/arti/internal/type_hints.py:168: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]
+ src/arti/types/bigquery.py:266: error: Argument 2 to "isinstance" has incompatible type "UnionType | <typing special form> | type"; expected "_ClassInfo"  [arg-type]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant