tartley / colorama

Simple cross-platform colored terminal text in Python
BSD 3-Clause "New" or "Revised" License
3.55k stars 253 forks source link

Colorama's custom AnsiCodes doesn't work with typing module #91

Open Mahi opened 8 years ago

Mahi commented 8 years ago

Apparently colorama uses a custom AnsiCodes class instead of Enum for its data. Unfortunately this causes colorama to not work with Python's typing module due to this line.

Example:

>>> from colorama import Back
>>> from typing import Optional
>>> def f(color: Optional[Back]=Back.RED):
...     pass
...
TypeError: Optional[t] requires a single type. Got <colorama.ansi.AnsiBack object at 0x00EC5ED0>.
wiggin15 commented 8 years ago

Since colorama supports both Python 2 and 3, using the Enum type (introduced in Python 3.4) is not an option as far as I know. There is also an issue here because Back is an instance of AnsiCodes (so not really a type) with modified attributes computed at runtime - so even if enum was available, this would not be a simple fix. Do you have other ideas on how to fix this? My only solution is using "str" as the type hint for now.