Skip to content

working out types of os.path function arguments fails #22590

Backlog

Description

@hramrach

Bug Report

A call to os.path.join that can be executed is rejected by mypy

To Reproduce

import os

def pa(d: bytes|str):
    fn = 'somefile' if isinstance(d, str) else b'somefile'
    return os.path.join(d, fn)

print(pa('a'))

Expected Behavior

mypy test.py

Actual Behavior

mypy test.py
test.py:5: error: Argument 1 to "join" has incompatible type "bytes | str"; expected "str" [arg-type]
test.py:5: error: Argument 2 to "join" has incompatible type "str | bytes"; expected "str" [arg-type]
Found 2 errors in 1 file (checked 1 source file)

python3.11 test.py
a/somefile

Your Environment

  • Mypy version used: mypy 2.3.1 (compiled: yes)
  • Mypy command-line flags: -
  • Mypy configuration options from mypy.ini (and other config files): -
  • Python version used: 3.11

Activity

  1. added
    bug
    on Dec 2, 2026
  2. AshutoshSingh840 commented on Dec 6, 2026

    @AshutoshSingh840

    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.

  3. hramrach commented on Dec 6, 2026

    @hramrach
    Author

    I found using a bound variable helps in many cases, including the simplistic case above:

    import os
    
    from typing import TypeVar
    
    BytesStr = TypeVar('BytesStr', bytes, str)
    
    def pa(d: BytesStr):
        fn: BytesStr = 'somefile' if isinstance(d, str) else b'somefile'
        return os.path.join(d, fn)
    
    print(pa('a'))
    

    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.

  4. hramrach commented on Dec 6, 2026

    @hramrach
    Author

    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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions