You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromtypingimportAnydeffunc() ->Any:
...
var=func()
reveal_type(var) # Anyassertisinstance(var, bool)
reveal_type(var) # boolifvar:
reveal_type(var) # Literal[True]passreveal_type(var) # Any -> should still be 'bool'!
The type should not be lost if it can't be narrowed to Literal[True] or Literal[False]. It's a side effect of #10389. The issue was already mentioned #10389 (comment) but hasn't been addressed yet.
Ok, looks like I was able to debug this problem. It is not really related to #10389, but this PR just made this problem visible. But, theoretically you can come up with other examples. Like:
fromtypingimportAnyvar: Anyreveal_type(var) # N: Revealed type is "Any"assertisinstance(var, bool) orisinstance(var, str)
reveal_type(var) # N: Revealed type is "Union[builtins.bool, builtins.str]"ifisinstance(var, bool):
reveal_type(var) # N: Revealed type is "builtins.bool"# Should be `Union[builtins.bool, builtins.str]`, but:reveal_type(var) # N: Revealed type is "Any"
I guess we should write proper type joiner for frame object. Two frames with Var(x): Literal[True] and Var(x): Literal[False] should be joined as Var(x): bool. The same with union types.
Activity
sobolevn commentedon Dec 25, 2021
Ok, looks like I was able to debug this problem. It is not really related to #10389, but this PR just made this problem visible. But, theoretically you can come up with other examples. Like:
What is the cause of this problem?
mypy/mypy/binder.py
Lines 232 to 237 in f96446c
allow_jump, it ends up with twoframeobjects inself.options_on_return[index]self.options_on_return[index]is only a part of the original typeif not all(is_same_type(type, cast(Type, t)) for t in resulting_values[1:]):it evaluates toTrueandAnyis returnedI guess we should write proper type joiner for
frameobject. Two frames withVar(x): Literal[True]andVar(x): Literal[False]should be joined asVar(x): bool. The same with union types.orsinium commentedon Dec 27, 2021
I have a related issue. Not sure if the same cause, though. In this code:
https://github.com/life4/deal/blob/48c6b1dbc51781f95ebba19c618f875ca9199ad4/deal/_runtime/_contracts.py#L67-L74
contractsvariable should be inferred asContractsaftercontracts = cls(func)but now it isOptional[Any].clsdoesn't help.contracts: Optional[Contracts], it stays that way, no type refinement is done bycontracts = cls(func)assert contracts is not None, it also doesn't help,contractsstaysOptional[Contracts].In mypy 0.910 everything was fine. In pyright everything is also correctly inferred.
brianschubert commentedon Dec 8, 2024
Fixed by #18138, which should be released with mypy v1.14.0
cdce8p commentedon Dec 9, 2024
Yes, it is. Opened #18272 to add the regression test from @sobolevn above to our test suite.
Add regression test for isinstance narrowing (#18272)