Skip to content

Commit 8bad227

Browse files
authored
Fix IncEx type alias to be compatible with mypy (#10813)
1 parent 4eecee7 commit 8bad227

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

pydantic/main.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@
7373

7474
# Keep these type aliases available at runtime:
7575
TupleGenerator: TypeAlias = Generator[Tuple[str, Any], None, None]
76-
# Keep this type alias in sync with the stub definition in `pydantic-core`:
77-
IncEx: TypeAlias = Union[
78-
Set[int], Set[str], Mapping[int, Union['IncEx', Literal[True]]], Mapping[str, Union['IncEx', Literal[True]]]
79-
]
76+
# NOTE: In reality, `bool` should be replaced by `Literal[True]` but mypy fails to correctly apply bidirectional
77+
# type inference (e.g. when using `{'a': {'b': True}}`):
78+
# NOTE: Keep this type alias in sync with the stub definition in `pydantic-core`:
79+
IncEx: TypeAlias = Union[Set[int], Set[str], Mapping[int, Union['IncEx', bool]], Mapping[str, Union['IncEx', bool]]]
8080

8181
_object_setattr = _model_construction.object_setattr
8282

tests/typechecking/misc.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ class Model(BaseModel):
1212

1313
def func(model: Model) -> None:
1414
model.model_dump(
15-
include={'a': {1: True}}, # type: ignore[arg-type]
15+
include={'a': {1: True}},
1616
)
1717
model.model_dump(
18-
include={'a': {'__all__': True}}, # type: ignore[arg-type]
18+
include={'a': {'__all__': True}},
1919
)
2020
model.model_dump(
2121
include={'a': {1: {'a'}}},
@@ -24,7 +24,7 @@ def func(model: Model) -> None:
2424
include={'a': {1, 2}},
2525
)
2626

27-
# Invalid cases:
27+
# Invalid cases, should fail but the `IncEx` alias uses `bool` due to mypy limitations:
2828
model.model_dump(
29-
include={'a': {1: False}}, # type: ignore[arg-type] # pyright: ignore[reportArgumentType]
29+
include={'a': {1: False}},
3030
)

0 commit comments

Comments
 (0)