fix(version): exec code with lazy type in dataclass#1869
Open
kiyoon wants to merge 5 commits intopypa:masterfrom
Open
fix(version): exec code with lazy type in dataclass#1869kiyoon wants to merge 5 commits intopypa:masterfrom
kiyoon wants to merge 5 commits intopypa:masterfrom
Conversation
ofek
approved these changes
Dec 25, 2024
Contributor
ofek
left a comment
There was a problem hiding this comment.
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!
Author
|
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 |
Author
Done!
Sure, let's add the example |
Author
|
Tests have been added. You may run the test with and without the change and see what happens! |
Author
|
@ofek When will this be reviewed? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1863