Skip to content

Commit 084c59e

Browse files
RonnyPfannschmidtCursor AIclaude
committed
refactor: use module-level TYPE_CHECKING imports for lazy annotation resolution
Replace aliased class imports (e.g. VcsEnvironment as _VcsEnvironment) under TYPE_CHECKING with module imports, then use module.Class in annotations. Combined with from __future__ import annotations, this gives proper type checking without Any escape hatches or runtime imports. Also fix missing assert statements in test_nested_from_active_contexts. Co-authored-by: Cursor AI <ai@cursor.sh> Co-authored-by: Anthropic Claude Opus 4 <claude@anthropic.com>
1 parent 8585974 commit 084c59e

3 files changed

Lines changed: 20 additions & 20 deletions

File tree

vcs-versioning/src/vcs_versioning/_environment.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
from typing import TYPE_CHECKING, Any, Literal
2020

2121
if TYPE_CHECKING:
22-
from ._config import Configuration
23-
from .overrides import EnvReader
22+
from . import _config, overrides
2423

2524
log = logging.getLogger(__name__)
2625

@@ -69,7 +68,7 @@ def log_level(self) -> int:
6968
return logging.WARNING
7069
return self.debug
7170

72-
def make_reader(self, dist_name: str | None = None) -> EnvReader:
71+
def make_reader(self, dist_name: str | None = None) -> overrides.EnvReader:
7372
"""Create an :class:`EnvReader` configured with this env's tool names."""
7473
from .overrides import EnvReader
7574

@@ -139,7 +138,7 @@ def from_env(
139138
_env=env,
140139
)
141140

142-
def build_config(self, **kwargs: Any) -> Configuration:
141+
def build_config(self, **kwargs: Any) -> _config.Configuration:
143142
"""Create a ``Configuration`` that carries this environment.
144143
145144
All *kwargs* are forwarded to ``Configuration.from_file``.

vcs-versioning/src/vcs_versioning/overrides.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717

1818
from __future__ import annotations
1919

20+
import contextvars
2021
import logging
2122
import os
2223
from collections.abc import Mapping, MutableMapping
2324
from contextlib import ContextDecorator
24-
from contextvars import ContextVar
2525
from datetime import datetime
2626
from typing import TYPE_CHECKING, Any, Literal, TypedDict, TypeVar, overload
2727

@@ -39,7 +39,7 @@
3939
if TYPE_CHECKING:
4040
from pytest import MonkeyPatch
4141

42-
from ._environment import VcsEnvironment as _VcsEnvironment
42+
from . import _environment
4343

4444
log = logging.getLogger(__name__)
4545

@@ -266,7 +266,7 @@ class GlobalOverrides:
266266

267267
def __init__(
268268
self,
269-
vcs_env: _VcsEnvironment,
269+
vcs_env: _environment.VcsEnvironment,
270270
tool: str,
271271
dist_name: str | None = None,
272272
additional_loggers: tuple[logging.Logger, ...] = (),
@@ -275,7 +275,7 @@ def __init__(
275275
self.tool = tool
276276
self.dist_name = dist_name
277277
self.additional_loggers = additional_loggers
278-
self._token: Any = None
278+
self._token: contextvars.Token[GlobalOverrides | None] | None = None
279279

280280
# ------------------------------------------------------------------
281281
# Backward-compatible properties delegating to vcs_env
@@ -466,8 +466,8 @@ def set_var(key: str, value: str) -> None:
466466

467467

468468
# Thread-local storage for active global overrides
469-
_active_overrides: ContextVar[GlobalOverrides | None] = ContextVar(
470-
"vcs_versioning_overrides", default=None
469+
_active_overrides: contextvars.ContextVar[GlobalOverrides | None] = (
470+
contextvars.ContextVar("vcs_versioning_overrides", default=None)
471471
)
472472

473473

@@ -515,7 +515,7 @@ def __init__(
515515
def __enter__(self) -> GlobalOverrides:
516516
"""Enter context: create GlobalOverrides if none is active."""
517517
# Check if there's already an active context
518-
existing = _active_overrides.get()
518+
existing: GlobalOverrides | None = _active_overrides.get()
519519

520520
if existing is not None:
521521
# Already have a context, just return it

vcs-versioning/testing_vcs/test_overrides_api.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,22 +139,23 @@ def test_nested_from_active_contexts() -> None:
139139

140140
with GlobalOverrides.from_env("TEST", env={"TEST_DEBUG": "DEBUG"}):
141141
# Original: DEBUG level
142-
assert _active_overrides.get() is not None
143-
assert _active_overrides.get().debug == logging.DEBUG # type: ignore[union-attr]
142+
def get_active() -> GlobalOverrides:
143+
res = _active_overrides.get()
144+
assert res is not None
145+
return res
146+
147+
assert get_active().debug == logging.DEBUG
144148

145149
with GlobalOverrides.from_active(debug=logging.INFO):
146150
# Modified: INFO level
147-
assert _active_overrides.get().debug == logging.INFO # type: ignore[union-attr]
148-
151+
assert get_active().debug == logging.INFO
149152
with GlobalOverrides.from_active(debug=logging.WARNING):
150153
# Further modified: WARNING level
151-
assert _active_overrides.get().debug == logging.WARNING # type: ignore[union-attr]
152-
154+
assert get_active().debug == logging.WARNING
153155
# Back to INFO
154-
assert _active_overrides.get().debug == logging.INFO # type: ignore[union-attr]
155-
156+
assert get_active().debug == logging.INFO
156157
# Back to DEBUG
157-
assert _active_overrides.get().debug == logging.DEBUG # type: ignore[union-attr]
158+
assert get_active().debug == logging.DEBUG
158159

159160

160161
def test_export_without_source_date_epoch() -> None:

0 commit comments

Comments
 (0)