gh-104879: Fix TypeAliasType.__module__ in exec() (#104881)

This commit is contained in:
Jelle Zijlstra 2023-05-24 11:48:13 -07:00 committed by GitHub
parent 1497607a8e
commit fe77a99fc8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 2 deletions

View file

@ -373,6 +373,20 @@ class TypeVarTests(BaseTestCase):
self.assertIs(T.__covariant__, False)
self.assertIs(T.__contravariant__, False)
self.assertIs(T.__infer_variance__, False)
self.assertEqual(T.__module__, __name__)
def test_basic_with_exec(self):
ns = {}
exec('from typing import TypeVar; T = TypeVar("T", bound=float)', ns, ns)
T = ns['T']
self.assertIsInstance(T, TypeVar)
self.assertEqual(T.__name__, 'T')
self.assertEqual(T.__constraints__, ())
self.assertIs(T.__bound__, float)
self.assertIs(T.__covariant__, False)
self.assertIs(T.__contravariant__, False)
self.assertIs(T.__infer_variance__, False)
self.assertIs(T.__module__, None)
def test_attributes(self):
T_bound = TypeVar('T_bound', bound=int)
@ -939,6 +953,17 @@ class TypeVarTupleTests(BaseTestCase):
Ts2 = TypeVarTuple('Ts2')
self.assertEqual(Ts2.__name__, 'Ts2')
def test_module(self):
Ts = TypeVarTuple('Ts')
self.assertEqual(Ts.__module__, __name__)
def test_exec(self):
ns = {}
exec('from typing import TypeVarTuple; Ts = TypeVarTuple("Ts")', ns)
Ts = ns['Ts']
self.assertEqual(Ts.__name__, 'Ts')
self.assertIs(Ts.__module__, None)
def test_instance_is_equal_to_itself(self):
Ts = TypeVarTuple('Ts')
self.assertEqual(Ts, Ts)
@ -7985,6 +8010,15 @@ class ParamSpecTests(BaseTestCase):
self.assertEqual(P, P)
self.assertIsInstance(P, ParamSpec)
self.assertEqual(P.__name__, 'P')
self.assertEqual(P.__module__, __name__)
def test_basic_with_exec(self):
ns = {}
exec('from typing import ParamSpec; P = ParamSpec("P")', ns, ns)
P = ns['P']
self.assertIsInstance(P, ParamSpec)
self.assertEqual(P.__name__, 'P')
self.assertIs(P.__module__, None)
def test_valid_uses(self):
P = ParamSpec('P')