Skip to content

Commit 758612b

Browse files
committed
Fix type_params lifetime in symboltable
1 parent ecc7742 commit 758612b

File tree

2 files changed

+30
-36
lines changed

2 files changed

+30
-36
lines changed

Lib/test/test_typing.py

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2924,13 +2924,10 @@ def test_runtime_checkable_generic_non_protocol(self):
29242924
@runtime_checkable
29252925
class Foo[T]: ...
29262926

2927-
# TODO: RUSTPYTHON
2928-
@unittest.expectedFailure
29292927
def test_runtime_checkable_generic(self):
2930-
# @runtime_checkable
2931-
# class Foo[T](Protocol):
2932-
# def meth(self) -> T: ...
2933-
# pass
2928+
@runtime_checkable
2929+
class Foo[T](Protocol):
2930+
def meth(self) -> T: ...
29342931

29352932
class Impl:
29362933
def meth(self) -> int: ...
@@ -2945,9 +2942,9 @@ def method(self) -> int: ...
29452942
# TODO: RUSTPYTHON
29462943
@unittest.expectedFailure
29472944
def test_pep695_generics_can_be_runtime_checkable(self):
2948-
# @runtime_checkable
2949-
# class HasX(Protocol):
2950-
# x: int
2945+
@runtime_checkable
2946+
class HasX(Protocol):
2947+
x: int
29512948

29522949
class Bar[T]:
29532950
x: T
@@ -2987,22 +2984,20 @@ def f():
29872984

29882985
self.assertIsInstance(f, HasCallProtocol)
29892986

2990-
# TODO: RUSTPYTHON
2991-
@unittest.expectedFailure
29922987
def test_no_inheritance_from_nominal(self):
29932988
class C: pass
29942989

2995-
# class BP(Protocol): pass
2990+
class BP(Protocol): pass
29962991

2997-
# with self.assertRaises(TypeError):
2998-
# class P(C, Protocol):
2999-
# pass
3000-
# with self.assertRaises(TypeError):
3001-
# class Q(Protocol, C):
3002-
# pass
3003-
# with self.assertRaises(TypeError):
3004-
# class R(BP, C, Protocol):
3005-
# pass
2992+
with self.assertRaises(TypeError):
2993+
class P(C, Protocol):
2994+
pass
2995+
with self.assertRaises(TypeError):
2996+
class Q(Protocol, C):
2997+
pass
2998+
with self.assertRaises(TypeError):
2999+
class R(BP, C, Protocol):
3000+
pass
30063001

30073002
class D(BP, C): pass
30083003

@@ -3014,7 +3009,7 @@ class E(C, BP): pass
30143009
# TODO: RUSTPYTHON
30153010
@unittest.expectedFailure
30163011
def test_no_instantiation(self):
3017-
# class P(Protocol): pass
3012+
class P(Protocol): pass
30183013

30193014
with self.assertRaises(TypeError):
30203015
P()
@@ -3042,16 +3037,14 @@ class CG(PG[T]): pass
30423037
with self.assertRaises(TypeError):
30433038
CG[int](42)
30443039

3045-
# TODO: RUSTPYTHON
3046-
@unittest.expectedFailure
30473040
def test_protocol_defining_init_does_not_get_overridden(self):
30483041
# check that P.__init__ doesn't get clobbered
30493042
# see https://bugs.python.org/issue44807
30503043

3051-
# class P(Protocol):
3052-
# x: int
3053-
# def __init__(self, x: int) -> None:
3054-
# self.x = x
3044+
class P(Protocol):
3045+
x: int
3046+
def __init__(self, x: int) -> None:
3047+
self.x = x
30553048
class C: pass
30563049

30573050
c = C()
@@ -3478,9 +3471,9 @@ def __getattr__(self, attr):
34783471
# TODO: RUSTPYTHON
34793472
@unittest.expectedFailure
34803473
def test_no_weird_caching_with_issubclass_after_isinstance_pep695(self):
3481-
# @runtime_checkable
3482-
# class Spam[T](Protocol):
3483-
# x: T
3474+
@runtime_checkable
3475+
class Spam[T](Protocol):
3476+
x: T
34843477

34853478
class Eggs[T]:
34863479
def __init__(self, x: T) -> None:

compiler/codegen/src/compile.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ fn eprint_location(zelf: &Compiler<'_>) {
230230
}
231231

232232
/// Better traceback for internal error
233+
#[track_caller]
233234
fn unwrap_internal<T>(zelf: &Compiler<'_>, r: InternalResult<T>) -> T {
234235
if let Err(ref r_err) = r {
235236
eprintln!("=== CODEGEN PANIC INFO ===");
@@ -1705,11 +1706,6 @@ impl Compiler<'_> {
17051706
func_flags |= bytecode::MakeFunctionFlags::CLOSURE;
17061707
}
17071708

1708-
// Pop the special type params symbol table
1709-
if type_params.is_some() {
1710-
self.pop_symbol_table();
1711-
}
1712-
17131709
self.emit_load_const(ConstantData::Code {
17141710
code: Box::new(code),
17151711
});
@@ -1728,6 +1724,11 @@ impl Compiler<'_> {
17281724
};
17291725
self.compile_normal_call(call);
17301726

1727+
// Pop the special type params symbol table
1728+
if type_params.is_some() {
1729+
self.pop_symbol_table();
1730+
}
1731+
17311732
self.apply_decorators(decorator_list);
17321733

17331734
self.store_name(name)

0 commit comments

Comments
 (0)