Skip to content

fix(version): exec code with lazy type in dataclass - #1869

Open
kiyoon wants to merge 7 commits into
pypa:masterfrom
kiyoon:fix/version-code-lazy-type-with-dataclass
Open

fix(version): exec code with lazy type in dataclass#1869
kiyoon wants to merge 7 commits into
pypa:masterfrom
kiyoon:fix/version-code-lazy-type-with-dataclass

Conversation

@kiyoon

@kiyoon kiyoon commented Dec 25, 2024

Copy link
Copy Markdown

Fixes #1863

@ofek ofek left a comment

Copy link
Copy Markdown
Contributor

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!

@ofek ofek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

@kiyoon

kiyoon commented Dec 25, 2024

Copy link
Copy Markdown
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 sys.modules["src/my_project/_version"] = module, it works.

@kiyoon

kiyoon commented Dec 25, 2024

Copy link
Copy Markdown
Author

Thanks! Can you please add a code comment explaining what this does?

Done!

After that then we can work on adding a test together!

Sure, let's add the example _version.py in the tests!

@kiyoon

kiyoon commented Dec 25, 2024

Copy link
Copy Markdown
Author

Tests have been added. You may run the test with and without the change and see what happens!

@kiyoon
kiyoon requested a review from ofek January 23, 2025 01:10
@kiyoon

kiyoon commented May 26, 2025

Copy link
Copy Markdown
Author

@ofek When will this be reviewed?

@cjames23

Copy link
Copy Markdown
Member

can you please run hatch fmt on your changes and add a new commit? I will review once you have a new commit that will allow our workflows to run tests

@kiyoon

kiyoon commented Aug 19, 2026

Copy link
Copy Markdown
Author

done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Using "PEP 563 – Postponed Evaluation of Annotations" in dataclasses produces error with version code.

3 participants