Skip to content

Document how to import types from stubs #8629

Closed
@ksamuel

Description

@ksamuel

I'm currently trying to type an API using the signal module. At some point, we pass a callback that has the type signal._Handler (see: https://github.com/python/typeshed/blob/master/stdlib/3/signal.pyi#L73), which is not in the signal module, but in typeshed.

If I try to import it, I get an error, and I could not find the proper way to do so in the documentation.

Activity

JukkaL

JukkaL commented on Apr 9, 2020

@JukkaL
Collaborator

Yes, it would be good to document this clearly, as the workaround is not obvious.

You can use the same approach used to deal with import cycles: https://mypy.readthedocs.io/en/stable/common_issues.html#import-cycles

kshitij86

kshitij86 commented on Jun 17, 2020

@kshitij86

@JukkaL I want to contribute to mypy. Could you assign this issue to me?

assigned and unassigned on Jun 19, 2020
JukkaL

JukkaL commented on Jun 19, 2020

@JukkaL
Collaborator

@kshitij86 Your contribution is welcome! This is now assigned to you.

mfarrugi

mfarrugi commented on Sep 3, 2020

@mfarrugi

https://github.com/python/typeshed/blob/master/stdlib/2and3/_typeshed/__init__.pyi#L3-L13 is a good starting point for an explanation.

For any types that are only available in stubs, you must do

from typing import TYPE_CHECKING
if TYPE_CHECKING:
    # <import your stub only type>
gvanrossum

gvanrossum commented on Sep 3, 2020

@gvanrossum
Member

@mfarrugi If you want to give it a try you’re welcome.

added a commit that references this issue on Nov 28, 2020
8142da0
added a commit that references this issue on Jan 6, 2021
6e8c0cd
added a commit that references this issue on Jan 19, 2021
a7d4c67
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @ksamuel@JukkaL@mfarrugi@gvanrossum@kshitij86

    Issue actions

      Document how to import types from stubs · Issue #8629 · python/mypy