The need for the annotations to be valid Python expressions could generate a lot of otherwise unneeded (and ugly) import statements.
Two solutions seem appropriate on the first look:
- The hinting could be done by using strings, if the referenced objects are in another module.
def foo(processor: 'myapp.processors.Processor',
document: 'myapp.documents.Document')
- Type comments could be used for type hinting, instead of actual function annotations
def foo(x, # type: int
y) # type: float
Of course, the solutions I give are simple suggestions, I'm sure there is a better way to solve this problem.
Arguments against using actual imported names for type arguments:
- It's ugly to have lots of extra imports, only used for hinting
- The startup time of a lot of applications would become much slower - because importing lots of names from all over the place would require a lot more stuff to be loaded upfront.
- Higher changes of circular imports - it's really not a problem one would expect to introduce. One could imagine people choosing not to specify type hints, out of fear of circular imports, or people introducing circular imports, by mistake, when creating "useful" type hints.
The need for the annotations to be valid Python expressions could generate a lot of otherwise unneeded (and ugly) import statements.
Two solutions seem appropriate on the first look:
Of course, the solutions I give are simple suggestions, I'm sure there is a better way to solve this problem.
Arguments against using actual imported names for type arguments: