When I try
type AnyStr = (str, bytes)
I get
$ mypy --version
mypy 1.19.1 (compiled: yes)
$ mypy huh.py
huh.py:1: error: Invalid type alias: expression is not a valid type [valid-type]
When I try
type AnyStr = (str, bytes)
I get
$ mypy --version
mypy 1.19.1 (compiled: yes)
$ mypy huh.py
huh.py:1: error: Invalid type alias: expression is not a valid type [valid-type]
AnyStr is a constrained type variable, not a type alias. You can declare a function, class, or type alias generic over a type variable equivalent to AnyStr as follows:
def concat[AnyStr: (str, bytes)](x: AnyStr, y: AnyStr) -> AnyStr:
return x + y
class C[AnyStr: (str, bytes)]:
def __init__(self, x: AnyStr) -> None:
self.x = x
type SpecificList[AnyStr: (str, bytes)] = list[AnyStr]
I’ve honestly never thought that AnyStr was a good name anyways; the typing docs list two separate things that the construct’s name could be confused for. Seeing as it’s been deprecated since 3.13, you might have a good opportunity to move away from it entirely:
def concat[T: (str, bytes)](x: T, y: T) -> T:
return x + y