Skip to content

allow using modules as a type #11518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DetachHead opened this issue Nov 11, 2021 · 2 comments
Closed

allow using modules as a type #11518

DetachHead opened this issue Nov 11, 2021 · 2 comments
Labels

Comments

@DetachHead
Copy link
Contributor

DetachHead commented Nov 11, 2021

Feature

from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from Sandpit import foo


def asdf() -> foo: # or Module[foo]
    from Sandpit import foo

    return foo

Pitch
this pattern is useful as a quick hack for resolving circular imports, also pycharm allows it

@erictraut
Copy link

I don't think modules should be allowed in the type system. The semantics of a module type are not well defined.

PEP 544 provides a potential solution here. It allows a protocol to describe a module's interface. This is how this would be solved in other languages like TypeScript.

It looks like mypy doesn't support this part of PEP 544 though. See this issue for details.

The other solution, of course, is to eliminate the circular dependency in your code. ;)

@emmatyping
Copy link
Member

I think in the past people have used types.ModuleType then used hasattr etc. But I would agree that modules as types are tricky, and I think #5018 is a better solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants