Skip to content

I'm looking for how can I to type adequately the return value of __import__(module) #1975

Answered by hauntsaninja
frodo821 asked this question in Q&A
Discussion options

You must be logged in to vote

There aren't great options...

  • You could define a Protocol and use that as your return type. This will work well, except it can be very boilerplate-y, depending on the API surface of the model.
    • This is basically the only thing that will work if your patch changes types
  • You could remove the return in your patch and rely on the side effect, so that import my.module will preserve the original modules types
  • You could also do an if TYPE_CHECKING hack at the call site, but this will be not so ergonomic

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@frodo821
Comment options

Answer selected by frodo821
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants