Skip to content

Regression of os.scandir() typecheck #11964

Open
@achimnol

Description

@achimnol

Bug Report

Type variable inference related with os.scandir() seems to generate false-positives.

To Reproduce

Here is a minimal reproduction example, which is accepted by mypy 0.910 but rejected by 0.930 and 0.931.

from __future__ import annotations

import os
from pathlib import Path


def _calc_usage(target_path: Path | os.DirEntry) -> None:
    with os.scandir(target_path) as scanner:
        for entry in scanner:
            if entry.is_dir():
                _calc_usage(target_path)
            else:
                print('f')


_calc_usage(Path('.'))

Expected Behavior

Success: no issues found in 1 source file

Actual Behavior

test.py:8: error: Value of type variable "AnyStr" of "scandir" cannot be "Union[DirEntry[Any], Any]"

Your Environment

  • Mypy version used: 0.931
  • Mypy command-line flags: python -m mypy test.py
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.9.6
  • Operating system and version: macOS 12.1

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions