Skip to content

Tag: typechecking

Subclassing Sequence with proper type hints in Python

I’m trying to implement a kind of custom sequence class in Python: Now I want to check that mypy is aware that elements of MySequence are items of type T: So it fails: mypy knows nothing about items of foo. The same example for ordinary Sequence works: If I’m trying to add type annotations to __ge…

Use isinstance to test for Unicode string

How can I do something like: But I would like isinstance to return True for this Unicode encoded string. Is there a Unicode string object type? Answer Test for str: or, if you must handle bytestrings, test for bytes separately: The two types are deliberately not exchangible; use explicit encoding (for str -&g…