Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions backend/src/hatchling/version/source/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def get_version_data(self) -> dict:
spec = spec_from_file_location(os.path.splitext(path)[0], path)
module = module_from_spec(spec) # type: ignore[arg-type]

# This fixes using PEP 563 (__future__ annotations) with dataclasses.
# https://github.com/pypa/hatch/issues/1863
sys.modules[os.path.splitext(path)[0]] = module

old_search_paths = list(sys.path)
try:
sys.path[:] = [*absolute_search_paths, *old_search_paths]
Expand Down
62 changes: 62 additions & 0 deletions tests/backend/version/source/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,65 @@ def foo(version_info):

with temp_dir.as_cwd():
assert source.get_version_data()['version'] == '1.0.0.1.dev0'


def test_pep563_with_dataclasses_1(temp_dir, helpers):
"""
Test postponed evaluation of annotations (using __future__) with dataclasses.

References:
- https://github.com/pypa/hatch/issues/1863
"""
source = CodeSource(str(temp_dir), {'path': 'a/b.py'})

file_path = temp_dir / 'a' / 'b.py'
file_path.ensure_parent_dir_exists()
file_path.write_text(
helpers.dedent(
"""
from __future__ import annotations

from dataclasses import dataclass

@dataclass
class VersionConfig:
test_dir: str | None = None
verbose: bool = False

__version__ = "0.1.1"
"""
)
)

with temp_dir.as_cwd():
assert source.get_version_data()['version'] == '0.1.1'


def test_pep563_with_dataclasses_2(temp_dir, helpers):
"""
Test postponed evaluation of annotations (using "type" string) with dataclasses.

References:
- https://github.com/pypa/hatch/issues/1863
"""
source = CodeSource(str(temp_dir), {'path': 'a/b.py'})

file_path = temp_dir / 'a' / 'b.py'
file_path.ensure_parent_dir_exists()
file_path.write_text(
helpers.dedent(
"""
from dataclasses import dataclass

@dataclass
class VersionConfig:
test_dir: "str | None" = None
verbose: bool = False

__version__ = "0.1.1"
"""
)
)

with temp_dir.as_cwd():
assert source.get_version_data()['version'] == '0.1.1'