Skip to content

Commit c353380

Browse files
fix: Remove None-related special-casing during inference with literal contexts
Signed-off-by: Edgar Ramírez Mondragón <edgarrm358@gmail.com>
1 parent 0719acc commit c353380

4 files changed

Lines changed: 53 additions & 25 deletions

File tree

‎mypy/checkexpr.py‎

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,7 @@
210210
remove_dups,
211211
split_with_prefix_and_suffix,
212212
)
213-
from mypy.types_utils import (
214-
is_generic_instance,
215-
is_overlapping_none,
216-
is_self_type_like,
217-
remove_optional,
218-
)
213+
from mypy.types_utils import is_generic_instance, is_self_type_like, remove_optional
219214
from mypy.typestate import type_state
220215
from mypy.typevars import fill_typevars
221216
from mypy.visitor import ExpressionVisitor
@@ -2151,25 +2146,6 @@ def infer_function_type_arguments_using_context(
21512146
# valid results.
21522147
erased_ctx = replace_meta_vars(ctx, ErasedType())
21532148
ret_type = callable.ret_type
2154-
if is_overlapping_none(ret_type) and is_overlapping_none(ctx):
2155-
# If both the context and the return type are optional, unwrap the optional,
2156-
# since in 99% cases this is what a user expects. In other words, we replace
2157-
# Optional[T] <: Optional[int]
2158-
# with
2159-
# T <: int
2160-
# while the former would infer T <: Optional[int].
2161-
ret_type = remove_optional(ret_type)
2162-
erased_ctx = remove_optional(erased_ctx)
2163-
#
2164-
# TODO: Instead of this hack and the one below, we need to use outer and
2165-
# inner contexts at the same time. This is however not easy because of two
2166-
# reasons:
2167-
# * We need to support constraints like [1 <: 2, 2 <: X], i.e. with variables
2168-
# on both sides. (This is not too hard.)
2169-
# * We need to update all the inference "infrastructure", so that all
2170-
# variables in an expression are inferred at the same time.
2171-
# (And this is hard, also we need to be careful with lambdas that require
2172-
# two passes.)
21732149
proper_ret = get_proper_type(ret_type)
21742150
if (
21752151
isinstance(proper_ret, TypeVarType)

‎test-data/unit/check-enum.test‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3007,3 +3007,22 @@ match foo:
30073007
foo()
30083008
reveal_type(foo) # N: Revealed type is "Literal[__main__.Foo.FOO2]"
30093009
[builtins fixtures/primitives.pyi]
3010+
3011+
[case testEnumLiteralInUnionContextWithGenericFallback]
3012+
# https://github.com/python/mypy/issues/22334
3013+
from enum import Enum
3014+
from typing import Literal, TypeVar
3015+
3016+
T = TypeVar("T")
3017+
3018+
class E(Enum):
3019+
SPECIAL = 1
3020+
3021+
def none_if_special(value: T | Literal[E.SPECIAL]) -> T | None:
3022+
return None if value is E.SPECIAL else value
3023+
3024+
def func() -> None:
3025+
x: str | Literal[E.SPECIAL] | None = E.SPECIAL
3026+
x = none_if_special(x)
3027+
reveal_type(x) # N: Revealed type is "builtins.str | None"
3028+
[builtins fixtures/tuple.pyi]

‎test-data/unit/check-literal.test‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3164,3 +3164,18 @@ bar: Literal[
31643164
]
31653165
reveal_type(bar) # N: Revealed type is "Literal['duration[s][pyarrow]'] | Literal['duration[ms][pyarrow]']"
31663166
[builtins fixtures/primitives.pyi]
3167+
3168+
[case testLiteralInUnionContextWithGenericFallback]
3169+
# https://github.com/python/mypy/issues/22334
3170+
from typing import Literal, TypeVar
3171+
3172+
T = TypeVar("T")
3173+
3174+
def none_if_one(value: T | Literal[1]) -> T | None:
3175+
return None if isinstance(value, int) else value
3176+
3177+
def func() -> None:
3178+
x: str | Literal[1] | None = 1
3179+
x = none_if_one(x)
3180+
reveal_type(x) # N: Revealed type is "builtins.str | None"
3181+
[builtins fixtures/tuple.pyi]

‎test-data/unit/check-sentinels.test‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,21 @@ reveal_type(X) # N: Revealed type is "X"
266266
reveal_type(inspect_sentinel) # N: Revealed type is "def (arg: typing_extensions.Sentinel)"
267267
inspect_sentinel(arg=X)
268268
[builtins fixtures/tuple.pyi]
269+
270+
[case testSentinelInUnionContextWithGenericFallback]
271+
# https://github.com/python/mypy/issues/22334
272+
from typing import Generic, TypeVar, reveal_type
273+
from typing_extensions import sentinel
274+
275+
Undefined = sentinel("Undefined")
276+
277+
_T = TypeVar("_T")
278+
279+
def none_if_undefined(value: _T | Undefined) -> _T | None:
280+
return None if value is Undefined else value
281+
282+
def func() -> None:
283+
device_id: str | Undefined | None = Undefined
284+
device_id = none_if_undefined(device_id)
285+
reveal_type(device_id) # N: Revealed type is "builtins.str | None"
286+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)