I have been adding type information to my package's .py
files in order to support running mypy
against the package. Among other things allows to generate typeshed information for this, third party, package.
Since my package has to be Python 2.7 compatible, I use comments for the type information:
def __init__(self, s):
# type: (Text) -> None
but in order to run mypy
this requires me to import typing:
from typing import Text, IO, BinaryIO, Union
this causes two problems:
This will not work on Python 3.5.0 and 3.5.1 as it has a module
typing
but that doesn't includeText
. Installingtyping
from PyPI doesn't solve that. (And there are users that run the package on that version of Python).This make my package dependent on
typing
for 2.7/3.3/3.4 installs, requiring extra downloads and installs.I have my own
Union
types defined:StreamType = Union[BinaryIO, IO[str], StringIO] StreamTextType = Union[Text, StreamType]
the code for this would have to be conditionally executed depending on typing being available or not.
For the first problem, since I do not run mypy
under Python 3.5.0/1, I can do something like:
import sys
if sys.version_info < (3, 5, 0) and sys.version_info >= (3, 5, 2):
from typing import Text, IO, BinaryIO, Union
but that doesn't solve the second issue.
Commenting out the import
, like the type information that is in comments,
# from typing import Text, IO, BinaryIO, Union
will cause mypy
to throw an error Name 'Text' is not defined
.
The third issue can be solve by using a try
-except
(ugly, and maybe also inefficient) or e.g. by testing against an environment variable (which could also be used to solve the first problem).
Is there an environment variable set when running mypy
, that I can test against, so that the import statement is only executed when running mypy
?
Testing against an environment variable would also allow me to put the definition of my own types within that "guarded".
Or some other solution?