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
classA: ...
classB: ...
classC(A, B): ...
deff(a: A) ->B: ...
defg(b: B) ->None:
ifisinstance(b, A):
reveal_type(b)
b=f(B())
g(C())
It seems that mypy assumes that is is impossible for the isinstance check to be true and does not evaluate what follows it. This code produces no errors. Even the reveal_type here is ignored. At the very least, the type checker should be aware of the existence of the class C, which inherits from both A and B, but that seems like it has a high potential for error. Better would just be to assume that it is possible for something to be an instance of both A and B, as someone could also create such a class in a separate file that imports this file, unless the type checker has a way to be sure the two types are mutually exclusive.
@ilevkivskyi had suggested in the similar issue #5423, that in strict mode, mypy should still type check unreachable code, but I would suggest, in addition to that, adding a warning for any code that mypy thinks is unreachable. Doing that might have allowed us to catch this example, which I found in mypy/server/astdiff.py: 165:
defsnapshot_definition(node: Optional[SymbolNode],
common: Tuple[object, ...]) ->Tuple[object, ...]:
"""Create a snapshot description of a symbol table node. The representation is nested tuples and dicts. Only externally visible attributes are included. """ifisinstance(node, FuncBase):
# TODO: infoifnode.type:
signature=snapshot_type(node.type)
else:
signature=snapshot_untyped_signature(node)
Note that snapshot_untyped_signature expects a Union[OverloadedFuncDef, FuncItem], and while it is true that OverloadedFuncDef and FuncItem are the only things that directly inherit from FuncBase, and FuncBase itself is abstract, I don't think the type checker is smart enough to know that, nor should it be. The issue here is that node is an Optional[SymbolNode], and while OverloadedFuncDef inherits from SymbolNode, FuncBase itself does not, so really we are expecting either an OverloadedFuncDef or a FuncDef here, but the type checker has no way of knowing that. PyCharm did give me a warning about this, and this created some problems when I was attempting to refactor some code in this function, which was using a slightly different check that mypy could not determine was unreachable, and so caused a type error where there wasn't one before, which was very confusing to me for a little while.
The text was updated successfully, but these errors were encountered:
Consider the following code:
It seems that mypy assumes that is is impossible for the
isinstance
check to be true and does not evaluate what follows it. This code produces no errors. Even thereveal_type
here is ignored. At the very least, the type checker should be aware of the existence of the classC
, which inherits from bothA
andB
, but that seems like it has a high potential for error. Better would just be to assume that it is possible for something to be an instance of bothA
andB
, as someone could also create such a class in a separate file that imports this file, unless the type checker has a way to be sure the two types are mutually exclusive.@ilevkivskyi had suggested in the similar issue #5423, that in strict mode, mypy should still type check unreachable code, but I would suggest, in addition to that, adding a warning for any code that mypy thinks is unreachable. Doing that might have allowed us to catch this example, which I found in
mypy/server/astdiff.py: 165
:Note that
snapshot_untyped_signature
expects aUnion[OverloadedFuncDef, FuncItem]
, and while it is true thatOverloadedFuncDef
andFuncItem
are the only things that directly inherit fromFuncBase
, andFuncBase
itself is abstract, I don't think the type checker is smart enough to know that, nor should it be. The issue here is thatnode
is anOptional[SymbolNode]
, and whileOverloadedFuncDef
inherits fromSymbolNode
,FuncBase
itself does not, so really we are expecting either anOverloadedFuncDef
or aFuncDef
here, but the type checker has no way of knowing that. PyCharm did give me a warning about this, and this created some problems when I was attempting to refactor some code in this function, which was using a slightly different check that mypy could not determine was unreachable, and so caused a type error where there wasn't one before, which was very confusing to me for a little while.The text was updated successfully, but these errors were encountered: