You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PEP 563 was adopted in Python 3.7, and may be able to make it easier for us to import types from other modules conditionally.
Modules can run this import to enable PEP 563:
from __future__ importannotations
This statement ensures that conditional type imports don't need to be surrounded in quotes when annotating a type. I will elaborate on what I mean by this in an example. Take this code:
from .persona_managerimportPersonaManagerdefget_manager() ->PersonaManager:
pass
Here we are just using PersonaManager to annotate the type of this function, but this also imports the entire persona_manager module even though it is not used here.
We can make this a conditional type import:
fromtypingimportTYPE_CHECKINGifTYPE_CHECKING:
from .persona_managerimportPersonaManagerdefget_manager() ->PersonaManager:
pass
However, this fails at runtime because the type annotations are somehow evaluated at runtime. This forces types from conditional imports to be surrounded in quotes:
...
defget_manager() ->'PersonaManager':
pass
With PEP 563, conditional type imports are slightly easier since the type no longer needs to be surrounded in quotes:
from __future__ importannotationsfromtypingimportTYPE_CHECKINGifTYPE_CHECKING:
from .persona_managerimportPersonaManagerdefget_manager() ->PersonaManager: # works at runtime too!pass
Additional context.
There are a couple of open questions, which is why I think we should explore first before taking action.
Does this have any impact beyond the jupyter_ai module? For example, does running from __future__ import annotations have any chance of breaking other extensions / packages in the environment?
Is there any way to enforce that import to ensure PEP 563 is followed?
The text was updated successfully, but these errors were encountered:
Uh oh!
There was an error while loading. Please reload this page.
Problem
PEP 563 was adopted in Python 3.7, and may be able to make it easier for us to import types from other modules conditionally.
Modules can run this import to enable PEP 563:
This statement ensures that conditional type imports don't need to be surrounded in quotes when annotating a type. I will elaborate on what I mean by this in an example. Take this code:
Here we are just using
PersonaManager
to annotate the type of this function, but this also imports the entirepersona_manager
module even though it is not used here.We can make this a conditional type import:
However, this fails at runtime because the type annotations are somehow evaluated at runtime. This forces types from conditional imports to be surrounded in quotes:
With PEP 563, conditional type imports are slightly easier since the type no longer needs to be surrounded in quotes:
Additional context.
There are a couple of open questions, which is why I think we should explore first before taking action.
Does this have any impact beyond the
jupyter_ai
module? For example, does runningfrom __future__ import annotations
have any chance of breaking other extensions / packages in the environment?Is there any way to enforce that import to ensure PEP 563 is followed?
The text was updated successfully, but these errors were encountered: