-
-
Notifications
You must be signed in to change notification settings - Fork 315
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
fix(version): exec code with lazy type in dataclass #1869
base: master
Are you sure you want to change the base?
fix(version): exec code with lazy type in dataclass #1869
Conversation
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.
Thanks! Can you please add a code comment explaining what this does? I don't understand either in fact. After that then we can work on adding a test together!
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.
.
Test is very simple. As in the issue, from __future__ import annotations
from dataclasses import dataclass
@dataclass
class VersionConfig:
test_dir: str | None = None
verbose: bool = False
__version__ = "0.0.0" from dataclasses import dataclass
@dataclass
class VersionConfig:
test_dir: "str | None" = None
verbose: bool = False
__version__ = "0.0.0" These two files won't be executed with this code: import sys
from importlib.util import module_from_spec, spec_from_file_location
spec = spec_from_file_location(
"src/my_project/_version", "src/my_project/_version.py"
)
module = module_from_spec(spec) # type: ignore[arg-type]
# sys.modules["src/my_project/_version"] = module
spec.loader.exec_module(module) # type: ignore[union-attr]
version = eval("__version__", vars(module))
print(version) but without the lazy type evaluation, # NO IMPORT __future__ annotations
from dataclasses import dataclass
@dataclass
class VersionConfig:
test_dir: str | None = None # HERE
verbose: bool = False
__version__ = "0.0.0" or, with the added line |
Done!
Sure, let's add the example |
Tests have been added. You may run the test with and without the change and see what happens! |
Fixes #1863