Skip to content

Cannot redefine "AnyStr" as a type variable #21332

@cpburnz

Description

@cpburnz

Bug Report

typing.AnyStr was deprecated in 3.13, and will be removed in 3.18. Mypy complains if you try defining AnyStr yourself.

To Reproduce

from typing import TypeVar
try:
	from typing import AnyStr  # Removed in 3.18.
except ImportError:
	AnyStr = TypeVar('AnyStr', str, bytes)

Gist URL: https://gist.github.com/mypy-play/ddfa044897f92fa6ca64aa8e8f2d9cef
Playground URL: https://mypy-play.net/?mypy=latest&python=3.14&gist=ddfa044897f92fa6ca64aa8e8f2d9cef

Expected Behavior

Mypy should no longer complain about defining AnyStr because it is deprecated.

Actual Behavior

Running mypy produce this error:

main.py:5: error: Cannot redefine "AnyStr" as a type variable  [misc]
main.py:5: error: Invalid assignment target  [misc]
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.20.2
  • Mypy command-line flags: None (default settings)
  • Mypy configuration options from mypy.ini (and other config files): None (default settings)
  • Python version used: 3.14.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions