False positive when union type is asserted to one type
Author: giommasoCreated Oct 1, 2026Updated Oct 1, 2026
Labelsbug
Describe the bug
Pyright reports a reportArgumentType error when using a variable declared as union typeA | None, even after the variable has been asserted to be of typeA
Code or Screenshots
My setup looks like a complicated version of this:
types.py:
# This is actually a protobuf generated file, protobuf version 4.21.12
class TypeA(enum.IntEnum)
VAL1 = 1
...
VAL1 = TypeA.VAL1
...main.py:
import types
def bar(t: types.TypeA):
...
def foo(t: types.TypeA | None):
assert t is types.VAL1
bar(t)This outputs:
error: Argument of type "TypeA | None" cannot be assigned to parameter "t" of type "TypeA" in function "bar"
Type "TypeA | None" is not assignable to type "TypeA"
"None" is not assignable to "TypeA"To avoid this reporting, this works:
def foo(t: TypeA | None):
assert t is not None
assert t is types.VAL1
bar(t)I couldn't reproduce the same issue with simplified versions, except by having a "wrong" assert. Which sounds like in my case the variable is resolved, but not to the right type, or something is lost along the way about its typing.
VS Code extension or command-line
Tested with the command-line tool, versions 1.1.401 and 1.1.414
Source: microsoft/pyright