Skip to content
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

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

Open
kiyoon opened this issue Dec 20, 2024 · 1 comment · May be fixed by #1869

Comments

@kiyoon
Copy link

kiyoon commented Dec 20, 2024

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "ml-project"
dynamic = ["version"]
requires-python = ">=3.10,<4"

[tool.hatch.version]
source = "code"
path = "src/ml_project/_version.py"

And the src/ml_project/_version.py

from __future__ import annotations

from dataclasses import dataclass

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

__version__ = "0.0.0"

or, use constant string for lazy type evaluation

from dataclasses import dataclass

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

__version__ = "0.0.0"

Produces error:

$ hatch build
──────────────────────────────────────────────────────────────────────────────────────────────────────── sdist ────────────────────────────────────────────────────────────────────────────────────────────────────────
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/kiyoon/Library/Application Support/hatch/env/virtual/ml-project/X6XZjsly/ml-project-build/lib/python3.11/site-packages/hatchling/__main__.py", line 6, in <module>
    sys.exit(hatchling())
             ^^^^^^^^^^^
  File "/Users/kiyoon/Library/Application Support/hatch/env/virtual/ml-project/X6XZjsly/ml-project-build/lib/python3.11/site-packages/hatchling/cli/__init__.py", line 26, in hatchling
    command(**kwargs)
  File "/Users/kiyoon/Library/Application Support/hatch/env/virtual/ml-project/X6XZjsly/ml-project-build/lib/python3.11/site-packages/hatchling/cli/build/__init__.py", line 82, in build_impl
    for artifact in builder.build(
  File "/Users/kiyoon/Library/Application Support/hatch/env/virtual/ml-project/X6XZjsly/ml-project-build/lib/python3.11/site-packages/hatchling/builders/plugin/interface.py", line 90, in build
    self.metadata.validate_fields()
  File "/Users/kiyoon/Library/Application Support/hatch/env/virtual/ml-project/X6XZjsly/ml-project-build/lib/python3.11/site-packages/hatchling/metadata/core.py", line 265, in validate_fields
    _ = self.version
        ^^^^^^^^^^^^
  File "/Users/kiyoon/Library/Application Support/hatch/env/virtual/ml-project/X6XZjsly/ml-project-build/lib/python3.11/site-packages/hatchling/metadata/core.py", line 149, in version
    self._version = self._get_version()
                    ^^^^^^^^^^^^^^^^^^^
  File "/Users/kiyoon/Library/Application Support/hatch/env/virtual/ml-project/X6XZjsly/ml-project-build/lib/python3.11/site-packages/hatchling/metadata/core.py", line 244, in _get_version
    core_metadata = self.core
                    ^^^^^^^^^
  File "/Users/kiyoon/Library/Application Support/hatch/env/virtual/ml-project/X6XZjsly/ml-project-build/lib/python3.11/site-packages/hatchling/metadata/core.py", line 191, in core
    self._version = self._get_version(metadata)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kiyoon/Library/Application Support/hatch/env/virtual/ml-project/X6XZjsly/ml-project-build/lib/python3.11/site-packages/hatchling/metadata/core.py", line 248, in _get_version
    version = self.hatch.version.cached
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kiyoon/Library/Application Support/hatch/env/virtual/ml-project/X6XZjsly/ml-project-build/lib/python3.11/site-packages/hatchling/metadata/core.py", line 1456, in cached
    raise type(e)(message) from None
AttributeError: Error getting the version from source `code`: 'NoneType' object has no attribute '__dict__'. Did you mean: '_cached'?
@kiyoon kiyoon changed the title Bug: Using "PEP 563 – Postponed Evaluation of Annotations" produces error with version code. Bug: Using "PEP 563 – Postponed Evaluation of Annotations" in dataclasses produces error with version code. Dec 20, 2024
@kiyoon
Copy link
Author

kiyoon commented Dec 20, 2024

mkdocs/mkdocs#3141

Found the relevant issue and the solution!

spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module  # 👈 Here
spec.loader.exec_module(module)

@kiyoon kiyoon linked a pull request Dec 25, 2024 that will close this issue
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 a pull request may close this issue.

1 participant