Skip to content

typing.TypeVar #5834

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

Merged
merged 4 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/copilot-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ cd extra_tests
pytest -v

# Run the Python test module
cargo run --release -- -m test
cargo run --release -- -m test ${TEST_MODULE}
cargo run --release -- -m test test_unicode # to test test_unicode.py

# Run the Python test module with specific function
cargo run --release -- -m test test_unicode -k test_unicode_escape
```

### Determining What to Implement
Expand Down
4 changes: 0 additions & 4 deletions Lib/test/test_genericalias.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,6 @@ class MyType(type):
with self.assertRaises(TypeError):
MyType[int]

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_pickle(self):
alias = GenericAlias(list, T)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
Expand All @@ -286,8 +284,6 @@ def test_pickle(self):
self.assertEqual(loaded.__args__, alias.__args__)
self.assertEqual(loaded.__parameters__, alias.__parameters__)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_copy(self):
class X(list):
def __copy__(self):
Expand Down
4 changes: 0 additions & 4 deletions Lib/test/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,8 +877,6 @@ def eq(actual, expected, typed=True):
eq(x[NT], int | NT | bytes)
eq(x[S], int | S | bytes)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_union_pickle(self):
orig = list[T] | int
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
Expand All @@ -888,8 +886,6 @@ def test_union_pickle(self):
self.assertEqual(loaded.__args__, orig.__args__)
self.assertEqual(loaded.__parameters__, orig.__parameters__)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_union_copy(self):
orig = list[T] | int
for copied in (copy.copy(orig), copy.deepcopy(orig)):
Expand Down
50 changes: 2 additions & 48 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,6 @@ def test_alias(self):

class TypeVarTests(BaseTestCase):
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_basic_plain(self):
T = TypeVar('T')
# T equals itself.
Expand Down Expand Up @@ -405,7 +404,6 @@ def test_basic_with_exec(self):
self.assertIs(T.__module__, None)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_attributes(self):
T_bound = TypeVar('T_bound', bound=int)
self.assertEqual(T_bound.__name__, 'T_bound')
Expand Down Expand Up @@ -448,14 +446,11 @@ def test_typevar_subclass_type_error(self):
issubclass(T, int)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_constrained_error(self):
with self.assertRaises(TypeError):
X = TypeVar('X', int)
X

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_union_unique(self):
X = TypeVar('X')
Y = TypeVar('Y')
Expand Down Expand Up @@ -486,7 +481,6 @@ def test_union_constrained(self):
self.assertNotEqual(Union[A, str], Union[A])

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_repr(self):
self.assertEqual(repr(T), '~T')
self.assertEqual(repr(KT), '~KT')
Expand All @@ -502,7 +496,6 @@ def test_no_redefinition(self):
self.assertNotEqual(TypeVar('T', int, str), TypeVar('T', int, str))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_cannot_subclass(self):
with self.assertRaisesRegex(TypeError, NOT_A_BASE_TYPE % 'TypeVar'):
class V(TypeVar): pass
Expand All @@ -515,8 +508,6 @@ def test_cannot_instantiate_vars(self):
with self.assertRaises(TypeError):
TypeVar('A')()

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_bound_errors(self):
with self.assertRaises(TypeError):
TypeVar('X', bound=Union)
Expand All @@ -533,22 +524,16 @@ def test_missing__name__(self):
)
exec(code, {})

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_no_bivariant(self):
with self.assertRaises(ValueError):
TypeVar('T', covariant=True, contravariant=True)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_cannot_combine_explicit_and_infer(self):
with self.assertRaises(ValueError):
TypeVar('T', covariant=True, infer_variance=True)
with self.assertRaises(ValueError):
TypeVar('T', contravariant=True, infer_variance=True)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_var_substitution(self):
T = TypeVar('T')
subst = T.__typing_subst__
Expand Down Expand Up @@ -591,7 +576,6 @@ def test_many_weakrefs(self):
del vals

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_constructor(self):
T = TypeVar(name="T")
self.assertEqual(T.__name__, "T")
Expand Down Expand Up @@ -648,8 +632,6 @@ def test_constructor(self):
self.assertIs(T.__infer_variance__, True)

class TypeParameterDefaultsTests(BaseTestCase):
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_typevar(self):
T = TypeVar('T', default=int)
self.assertEqual(T.__default__, int)
Expand Down Expand Up @@ -844,8 +826,6 @@ class A(Generic[T, U, DefaultStrT]): ...
):
Test = A[int]

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_pickle(self):
global U, U_co, U_contra, U_default # pickle wants to reference the class by name
U = TypeVar('U')
Expand Down Expand Up @@ -3695,8 +3675,6 @@ def test_repr(self):
self.assertEqual(repr(MySimpleMapping),
f"<class '{__name__}.MySimpleMapping'>")

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_chain_repr(self):
T = TypeVar('T')
S = TypeVar('S')
Expand All @@ -3721,8 +3699,6 @@ class C(Generic[T]):
self.assertTrue(str(Z).endswith(
'.C[typing.Tuple[str, int]]'))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_new_repr(self):
T = TypeVar('T')
U = TypeVar('U', covariant=True)
Expand All @@ -3734,8 +3710,6 @@ def test_new_repr(self):
self.assertEqual(repr(List[S][T][int]), 'typing.List[int]')
self.assertEqual(repr(List[int]), 'typing.List[int]')

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_new_repr_complex(self):
T = TypeVar('T')
TS = TypeVar('TS')
Expand Down Expand Up @@ -3862,8 +3836,6 @@ def test_orig_bases(self):
class C(typing.Dict[str, T]): ...
self.assertEqual(C.__orig_bases__, (typing.Dict[str, T],))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_naive_runtime_checks(self):
def naive_dict_check(obj, tp):
# Check if a dictionary conforms to Dict type
Expand Down Expand Up @@ -3919,8 +3891,6 @@ class D(C, List[T][U][V]): ...
self.assertEqual(C.__orig_bases__, (List[T][U][V],))
self.assertEqual(D.__orig_bases__, (C, List[T][U][V]))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_subscript_meta(self):
T = TypeVar('T')
class Meta(type): ...
Expand Down Expand Up @@ -3972,8 +3942,6 @@ class A(Generic[T]):
self.assertTrue(repr(Tuple[mod_generics_cache.B.A[str]])
.endswith('mod_generics_cache.B.A[str]]'))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_extended_generic_rules_eq(self):
T = TypeVar('T')
U = TypeVar('U')
Expand All @@ -3990,8 +3958,6 @@ class Derived(Base): ...
self.assertEqual(Callable[[T], T][KT], Callable[[KT], KT])
self.assertEqual(Callable[..., List[T]][int], Callable[..., List[int]])

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_extended_generic_rules_repr(self):
T = TypeVar('T')
self.assertEqual(repr(Union[Tuple, Callable]).replace('typing.', ''),
Expand Down Expand Up @@ -4298,8 +4264,6 @@ class C(B[int]):
)
del PP

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_copy_and_deepcopy(self):
T = TypeVar('T')
class Node(Generic[T]): ...
Expand Down Expand Up @@ -8419,8 +8383,6 @@ def test_order_in_union(self):
with self.subTest(args=args):
self.assertEqual(expr2, Union[args])

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_specialize(self):
L = Annotated[List[T], "my decoration"]
LI = Annotated[List[int], "my decoration"]
Expand Down Expand Up @@ -8471,8 +8433,6 @@ def __eq__(self, other):
self.assertEqual(a.x, c.x)
self.assertEqual(a.classvar, c.classvar)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_instantiate_generic(self):
MyCount = Annotated[typing.Counter[T], "my decoration"]
self.assertEqual(MyCount([4, 4, 5]), {4: 2, 5: 1})
Expand Down Expand Up @@ -8601,8 +8561,6 @@ class _Annotated_test_G(Generic[T]):
self.assertEqual(x.bar, 'abc')
self.assertEqual(x.x, 1)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_subst(self):
dec = "a decoration"
dec2 = "another decoration"
Expand Down Expand Up @@ -8748,8 +8706,6 @@ def test_typevar_subst(self):
with self.assertRaises(TypeError):
J[int]

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_annotated_in_other_types(self):
X = List[Annotated[T, 5]]
self.assertEqual(X[int], List[Annotated[int, 5]])
Expand Down Expand Up @@ -9750,14 +9706,14 @@ class CustomerModel(ModelBase, init=False):


class NoDefaultTests(BaseTestCase):
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_pickling(self):
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
s = pickle.dumps(NoDefault, proto)
loaded = pickle.loads(s)
self.assertIs(NoDefault, loaded)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_constructor(self):
self.assertIs(NoDefault, type(NoDefault)())
with self.assertRaises(TypeError):
Expand All @@ -9775,8 +9731,6 @@ def test_doc(self):
def test_class(self):
self.assertIs(NoDefault.__class__, type(NoDefault))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_no_call(self):
with self.assertRaises(TypeError):
NoDefault()
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1498,7 +1498,7 @@ fn best_base<'a>(bases: &'a [PyTypeRef], vm: &VirtualMachine) -> PyResult<&'a Py
if !base_i.slots.flags.has_feature(PyTypeFlags::BASETYPE) {
return Err(vm.new_type_error(format!(
"type '{}' is not an acceptable base type",
base_i.name()
base_i.slot_name()
)));
}

Expand Down
Loading
Loading