Skip to content

Type hinting would generate a lot of import statements (possibly leading to circular imports). Use strings instead of imported names? #105

Description

@vladiibine

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:

  1. 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')
  1. 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:

  1. It's ugly to have lots of extra imports, only used for hinting
  2. 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.
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions