Skip to content

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

Open
@kiyoon

Description

@kiyoon
[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'?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions