Skip to content

Commit 477cddf

Browse files
fix: Fix ClassVarTypeHint._copy_with_args()
1 parent d0248ef commit 477cddf

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

.changelog/_unreleased.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[[entries]]
2+
id = "20ce9dbc-dcb5-432c-9a72-b0c085f70180"
3+
type = "fix"
4+
description = "Fix `ClassVarTypeHint._copy_with_args()`"
5+
author = "@NiklasRosenstein"

src/typeapi/typehint.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,3 +538,7 @@ def __init__(self, hint: object, source: "Any | None" = None) -> None:
538538
self._args = (self.hint.__type__,) # type: ignore[attr-defined]
539539
else:
540540
self._args = ()
541+
542+
def _copy_with_args(self, args: Tuple[Any, ...]) -> TypeHint:
543+
assert len(args) == 1, "a ClassVar type hint requires exactly one argument"
544+
return ClassVarTypeHint(ClassVar[args[0]])

src/typeapi/typehint_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,3 +602,8 @@ def test__TypeHint__with_ClassVar() -> None:
602602
assert hint.hint == ClassVar[Union[int, str]]
603603
assert hint.args == (Union[int, str],)
604604
assert hint[0] == TypeHint(Union[int, str])
605+
606+
607+
def test__ClassVarTypeHint__copy_with_args() -> None:
608+
hint = TypeHint(ClassVar[int])
609+
assert hint._copy_with_args((str,)).hint == ClassVar[str]

0 commit comments

Comments
 (0)