python / python/mypy

Error with mixing str and pathlib.Path in command line arguments list

Open
#13,004 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-join-v-union
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

mypy raises error if list of command line arguments (passing to subprocess.call) contains items of different types (for example, str and pathlib.Path). It can be fixed by adding type hint to variable, but it's too annoying.

To Reproduce

import pathlib
import subprocess

cmd = [pathlib.Path('bash'), '-c', 'echo Hello']
subprocess.call(cmd)

Expected Behavior

mypy should not raise any error.

Actual Behavior

main.py:5: error: Argument 1 to "check_call" has incompatible type "List[object]"; expected "Union[Union[str, bytes, PathLike[str], PathLike[bytes]], Sequence[Union[str, bytes, PathLike[str], PathLike[bytes]]]]"
Found 1 error in 1 file (checked 1 source file)

Why can't mypy recognize cmd type as List[Union[pathlib.Path, str]], not List[object]?

Environment

  • Mypy version used: 0.961
  • Python version used: 3.10
  • Operating system: Linux

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided Python reproducer and the typing of subprocess.call, then trace how mypy infers the type of a mixed list containing pathlib.Path and str. Confirm the change with a regression test using the shown command-line argument list, and verify that mypy reports no error while existing subprocess argument checks remain intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.