Skip to content

Commit 22acfc0

Browse files
committed
wip
1 parent bd14e29 commit 22acfc0

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

pyproject.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ main = ["poetry==1.7.1"]
2626
[tool.mypy]
2727
python_version = 3.9
2828
packages = ["basedtyping", "tests"]
29-
# we can't use override until we bump the minimum typing_extensions or something
30-
disable_error_code = ["explicit-override"]
3129

3230
[tool.ruff.format]
3331
skip-magic-trailing-comma = true

src/basedtyping/__init__.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
)
2828

2929
import typing_extensions
30-
from typing_extensions import Never, ParamSpec, Self, TypeAlias, TypeGuard, TypeVarTuple
30+
from typing_extensions import Never, ParamSpec, Self, TypeAlias, TypeGuard, \
31+
TypeVarTuple, override
3132

3233
from basedtyping import transformer
3334
from basedtyping.runtime_only import OldUnionType
@@ -69,13 +70,15 @@
6970
class _BasedSpecialForm(_SpecialForm, _root=True): # type: ignore[misc]
7071
_name: str
7172

73+
@override
7274
def __init_subclass__(cls, _root=False): # noqa: FBT002
7375
super().__init_subclass__(_root=_root) # type: ignore[call-arg]
7476

7577
def __init__(self, *args: object, **kwargs: object):
7678
self.alias = kwargs.pop("alias", _BasedGenericAlias)
7779
super().__init__(*args, **kwargs)
7880

81+
@override
7982
def __repr__(self) -> str:
8083
return "basedtyping." + self._name
8184

@@ -244,6 +247,7 @@ def _is_subclass(cls, subclass: object) -> TypeGuard[_ReifiedGenericMetaclass]:
244247
cast(_ReifiedGenericMetaclass, subclass)._orig_class(),
245248
)
246249

250+
@override
247251
def __subclasscheck__(cls, subclass: object) -> bool:
248252
if not cls._is_subclass(subclass):
249253
return False
@@ -264,6 +268,7 @@ def __subclasscheck__(cls, subclass: object) -> bool:
264268
subclass._check_generics_reified()
265269
return cls._type_var_check(subclass.__reified_generics__)
266270

271+
@override
267272
def __instancecheck__(cls, instance: object) -> bool:
268273
if not cls._is_subclass(type(instance)):
269274
return False
@@ -272,6 +277,7 @@ def __instancecheck__(cls, instance: object) -> bool:
272277
return cls._type_var_check(cast(ReifiedGeneric[object], instance).__reified_generics__)
273278

274279
# need the generic here for pyright. see https://github.com/microsoft/pyright/issues/5488
280+
@override
275281
def __call__(cls: type[T], *args: object, **kwargs: object) -> T:
276282
"""A placeholder ``__call__`` method that gets called when the class is
277283
instantiated directly, instead of first supplying the type parameters.
@@ -400,6 +406,7 @@ def __class_getitem__( # type: ignore[no-any-decorated]
400406
reified_generic_copy._can_do_instance_and_subclass_checks_without_generics = False
401407
return reified_generic_copy
402408

409+
@override
403410
def __init_subclass__(cls):
404411
cls._can_do_instance_and_subclass_checks_without_generics = True
405412
super().__init_subclass__()
@@ -473,14 +480,17 @@ def Untyped( # noqa: N802
473480

474481

475482
class _IntersectionGenericAlias(_BasedGenericAlias, _root=True):
483+
@override
476484
def copy_with(self, args: object) -> Self: # type: ignore[override] # TODO: put in the overloads # noqa: TD003
477485
return cast(Self, Intersection[args])
478486

487+
@override
479488
def __eq__(self, other: object) -> bool:
480489
if not isinstance(other, _IntersectionGenericAlias):
481490
return NotImplemented
482491
return set(self.__args__) == set(other.__args__)
483492

493+
@override
484494
def __hash__(self) -> int:
485495
return hash(frozenset(self.__args__))
486496

@@ -490,6 +500,7 @@ def __instancecheck__(self, obj: object) -> bool:
490500
def __subclasscheck__(self, cls: type[object]) -> bool:
491501
return all(issubclass(cls, arg) for arg in self.__args__)
492502

503+
@override
493504
def __reduce__(self) -> (object, object):
494505
func, (_, args) = super().__reduce__() # type: ignore[no-any-expr, misc]
495506
return func, (Intersection, args)
@@ -538,10 +549,13 @@ def Intersection(self: _BasedSpecialForm, parameters: object) -> object: # noqa
538549

539550

540551
class _TypeFormForm(_BasedSpecialForm, _root=True): # type: ignore[misc]
552+
# TODO: decorator-ify
541553
def __init__(self, doc: str):
554+
super().__init__()
542555
self._name = "TypeForm"
543556
self._doc = self.__doc__ = doc
544557

558+
@override
545559
def __getitem__(self, parameters: object | tuple[object]) -> _BasedGenericAlias:
546560
if not isinstance(parameters, tuple):
547561
parameters = (parameters,)
@@ -610,8 +624,8 @@ def __init__(self, arg: str, *, is_argument=True, module: object = None, is_clas
610624
except SyntaxError:
611625
# Callable: () -> int
612626
if "->" not in arg_to_compile:
613-
raise RuntimeError(
614-
f"expected a callable type, but found... what is {arg_to_compile}?"
627+
raise SyntaxError(
628+
f"invalid syntax in ForwardRef: {arg_to_compile}?"
615629
) from None
616630
code = compile("'un-representable callable type'", "<string>", "eval")
617631

@@ -625,6 +639,7 @@ def __init__(self, arg: str, *, is_argument=True, module: object = None, is_clas
625639

626640
if sys.version_info >= (3, 13):
627641

642+
@override
628643
def _evaluate(
629644
self,
630645
globalns: dict[str, object] | None,
@@ -637,6 +652,7 @@ def _evaluate(
637652

638653
elif sys.version_info >= (3, 12):
639654

655+
@override
640656
def _evaluate(
641657
self,
642658
globalns: dict[str, object] | None,
@@ -649,6 +665,7 @@ def _evaluate(
649665

650666
else:
651667

668+
@override
652669
def _evaluate(
653670
self,
654671
globalns: dict[str, object] | None,

src/basedtyping/transformer.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
from contextlib import contextmanager
1010
from enum import Enum
1111
from typing import cast
12+
from typing_extensions import override
1213

1314
import typing_extensions
1415

1516
import basedtyping
1617

1718

18-
# ruff: noqa: N802, S101
19+
# rasdfuff: noqa: N802, S101
1920
class CringeTransformer(ast.NodeTransformer):
2021
"""Transforms `1 | 2` into `Literal[1] | Literal[2]` etc"""
2122

@@ -48,6 +49,7 @@ def __init__(
4849
self.basedtyping_name: basedtyping,
4950
}
5051

52+
@override
5153
def visit(self, node: ast.AST) -> ast.AST:
5254
return cast(ast.AST, super().visit(node))
5355

@@ -107,6 +109,7 @@ def implicit_tuple(self, *, value=True) -> typing.Iterator[None]:
107109
finally:
108110
self._implicit_tuple = implicit_tuple
109111

112+
@override
110113
def visit_Subscript(self, node: ast.Subscript) -> ast.AST:
111114
node_type = self.eval_type(node.value)
112115
if self.eval_type(node.value) is typing_extensions.Literal:
@@ -133,6 +136,7 @@ def visit_Subscript(self, node: ast.Subscript) -> ast.AST:
133136
node = self.subscript(self._typing("Callable"), slice2_)
134137
return node
135138

139+
@override
136140
def visit_Attribute(self, node: ast.Attribute) -> ast.AST:
137141
node = self.generic_visit(node)
138142
assert isinstance(node, ast.expr)
@@ -142,12 +146,14 @@ def visit_Attribute(self, node: ast.Attribute) -> ast.AST:
142146
return self._literal(node)
143147
return node
144148

149+
@override
145150
def visit_Name(self, node: ast.Name) -> ast.AST:
146151
name_type = self.eval_type(node)
147152
if isinstance(name_type, Enum):
148153
return self._literal(node)
149154
return node
150155

156+
@override
151157
def visit_Constant(self, node: ast.Constant) -> ast.AST:
152158
value = cast(object, node.value)
153159
if not self.string_literals and isinstance(value, str):
@@ -156,13 +162,15 @@ def visit_Constant(self, node: ast.Constant) -> ast.AST:
156162
return self._literal(node)
157163
return node
158164

165+
@override
159166
def visit_Tuple(self, node: ast.Tuple) -> ast.AST:
160167
with self.implicit_tuple(value=False):
161168
result = self.generic_visit(node)
162169
if not self._implicit_tuple:
163170
return self.subscript(self._typing("Tuple"), cast(ast.expr, result))
164171
return result
165172

173+
@override
166174
def visit_Compare(self, node: ast.Compare) -> ast.AST:
167175
if len(node.ops) == 1 and isinstance(node.ops[0], ast.Is):
168176
result = self.subscript(
@@ -171,6 +179,7 @@ def visit_Compare(self, node: ast.Compare) -> ast.AST:
171179
return self.generic_visit(result)
172180
return self.generic_visit(node)
173181

182+
@override
174183
def visit_IfExp(self, node: ast.IfExp) -> ast.AST:
175184
if (
176185
isinstance(node.body, ast.Compare)
@@ -192,6 +201,7 @@ def visit_FunctionType(self, node: ast.FunctionType) -> ast.AST:
192201
ast.Tuple([ast.List(node.argtypes, ctx=ast.Load()), node.returns], ctx=ast.Load()),
193202
)
194203
)
204+
@override
195205

196206
def visit_BinOp(self, node: ast.BinOp) -> ast.AST:
197207
node = self.generic_visit(node)

0 commit comments

Comments
 (0)