-
I have the following function that I would like to use to convert either a mapping or a float to a mapping (with def asdict(value: Mapping[_K, _V] | _V) -> Mapping[_K, _V]:
if not isinstance(value, Mapping):
return defaultdict(lambda: value)
return value I get a warning from Why is the type deduce like that? Is there a way to have it deduce |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I presume that you have strict mode enabled because pyright does not generate any type errors for this code in standard mode. I'm guessing that A simplified version of the function should make it more clear why pyright narrows def func[_V](value: _V):
if isinstance(value, Mapping):
reveal_type(value) # Mapping[Unknown, Unknown] Here |
Beta Was this translation helpful? Give feedback.
I presume that you have strict mode enabled because pyright does not generate any type errors for this code in standard mode.
I'm guessing that
_K
and_V
are type variables with no explicit upper bound, in which case they have implicit upper bounds ofobject
. I can't tell from your code snippet wither_K
and_V
are scoped to a class. I'm guessing that they're not.A simplified version of the function should make it more clear why pyright narrows
_V
toMapping[Unknown, Unknown]
in this case.Here
value
could be of any type including aMapping
. The type arguments are unknown, s…