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
{{ message }}
Repository navigation
working out types of os.path function arguments fails #22590
I'd like to investigate and work on this issue. I'll first trace how mypy resolves the os.path.join overloads for union-typed arguments, identify the root cause, and then add a focused regression test with the fix. Please let me know if there are any pointers or existing investigations I should consider.
Also if I save the isinstance result (eg. because I want to assign multiple variables, or because it's class-wide) I get an error again:
import os
from typing import TypeVar
BytesStr = TypeVar('BytesStr', bytes, str)
def pa(d: BytesStr):
is_bytes = isinstance(d, str)
fn: BytesStr = 'somefile' if is_bytes else b'somefile'
return os.path.join(d, fn)
print(pa('a'))
mypy test.py
test.py:9: error: Incompatible types in assignment (expression has type "str | bytes", variable has type "bytes") [assignment]
test.py:9: error: Incompatible types in assignment (expression has type "str | bytes", variable has type "str") [assignment]
Found 2 errors in 1 file (checked 1 source file)
Activity
AshutoshSingh840 commentedon Dec 6, 2026
I'd like to investigate and work on this issue. I'll first trace how mypy resolves the
os.path.joinoverloads for union-typed arguments, identify the root cause, and then add a focused regression test with the fix. Please let me know if there are any pointers or existing investigations I should consider.hramrach commentedon Dec 6, 2026
I found using a bound variable helps in many cases, including the simplistic case above:
mypy test.py
Success: no issues found in 1 source file
The problem is that mypy cannot hold the binding to a specific type unless a type variable is explicitly created by the user.
hramrach commentedon Dec 6, 2026
Also if I save the isinstance result (eg. because I want to assign multiple variables, or because it's class-wide) I get an error again:
mypy test.py
test.py:9: error: Incompatible types in assignment (expression has type "str | bytes", variable has type "bytes") [assignment]
test.py:9: error: Incompatible types in assignment (expression has type "str | bytes", variable has type "str") [assignment]
Found 2 errors in 1 file (checked 1 source file)