-
Notifications
You must be signed in to change notification settings - Fork 122
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
Use get_type_hints() to resolve type annotations that are encoded as strings #424
Conversation
I don't think the pre-commit.ci failure is related? |
Looks good.
|
The relevant PEP says:
and
So, postponed annotations certainly remove the need for quoted forward references. The PEP also makes clear that using
Additionally, the mypy documentation states:
If this doesn't convince you, I would need to add a test that's only run on Python 3.7+ (where the future import is available). Tell me what you think! |
I am not sure exactly how One possibility is to have testing that depends on the Python version, but in the presence of side effects from that import (as I suspect is the case), I am not sure that this is a good idea. Related: |
In the announcement of Python 3.7 (where the future import was introduced), it says:
So this does seem to affect some deeper level of the compiler.
I don't think so. In my experience, it seems to work on a per-module level. Meaning that it doesn't matter if you import a future activated module from one that is not activated, or vice-versa. I think this is also how the If you have two modules a.py and b.py and they look like this: a.py def f(a: unknown_name):
pass b.py from __future__ import annotations
from a import f and you run b.py, it throws an error:
but if you change a.py to be from __future__ import annotations
def f(a: unknown_name):
pass it works. So the effect of the future import seems to be locally restricted. I really tried finding more detailed documentation for this, but wasn't much I could find. In general though, I think OmegaConf should use |
Cool, thanks for testing. Can you add such a test? you can condition it con Python 3.7+ before the import. |
I changed the test. Is this what you had in mind? A major difficulty is that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome.
One nit with the news file.
When using `from __future__ import annotations` (which will become default behavior in Python 3.10), type annotations are stored as strings. The standard library provides `typing.get_type_hints()` to convert these strings to types.
This has to be guarded by a check of the python version.
Done! |
Awesome, thanks! |
Closes #303
When using
from __future__ import annotations
(which will become default behavior in Python 3.10), type annotations are stored as strings. The standard library providestyping.get_type_hints()
to convert these strings to types.For the tests, I simulated this behavior by wrapping the type annotations in quotes:
This has the same effect and also works on Python 3.6. (The future import was introduced in Python 3.7.)
I was a bit unsure about the tests. Let me know if I can improve them.