Skip to content

Commit 0c31c5f

Browse files
committed
typing.NoDefault
1 parent 29e452a commit 0c31c5f

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

Lib/test/test_typing.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9706,14 +9706,14 @@ class CustomerModel(ModelBase, init=False):
97069706

97079707

97089708
class NoDefaultTests(BaseTestCase):
9709+
# TODO: RUSTPYTHON
9710+
@unittest.expectedFailure
97099711
def test_pickling(self):
97109712
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
97119713
s = pickle.dumps(NoDefault, proto)
97129714
loaded = pickle.loads(s)
97139715
self.assertIs(NoDefault, loaded)
97149716

9715-
# TODO: RUSTPYTHON
9716-
@unittest.expectedFailure
97179717
def test_constructor(self):
97189718
self.assertIs(NoDefault, type(NoDefault)())
97199719
with self.assertRaises(TypeError):
@@ -9731,8 +9731,6 @@ def test_doc(self):
97319731
def test_class(self):
97329732
self.assertIs(NoDefault.__class__, type(NoDefault))
97339733

9734-
# TODO: RUSTPYTHON
9735-
@unittest.expectedFailure
97369734
def test_no_call(self):
97379735
with self.assertRaises(TypeError):
97389736
NoDefault()

vm/src/stdlib/typing.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
pub(crate) use _typing::make_module;
1+
use crate::{PyRef, VirtualMachine, stdlib::PyModule};
2+
3+
pub(crate) use _typing::NoDefault;
4+
5+
pub(crate) fn make_module(vm: &VirtualMachine) -> PyRef<PyModule> {
6+
let module = _typing::make_module(vm);
7+
extend_module!(vm, &module, {
8+
"NoDefault" => vm.ctx.typing_no_default.clone(),
9+
});
10+
module
11+
}
212

313
#[pymodule]
414
pub(crate) mod _typing {
@@ -117,7 +127,7 @@ pub(crate) mod _typing {
117127
Ok(default_value.clone())
118128
} else {
119129
// Return NoDefault singleton
120-
vm.import("typing", 0)?.get_attr("NoDefault", vm)
130+
Ok(vm.ctx.typing_no_default.clone().into())
121131
}
122132
}
123133

@@ -146,12 +156,7 @@ pub(crate) mod _typing {
146156
return false;
147157
}
148158
// Check if default_value is NoDefault
149-
if let Ok(typing_module) = vm.import("typing", 0) {
150-
if let Ok(no_default) = typing_module.get_attr("NoDefault", vm) {
151-
return !default_value.is(&no_default);
152-
}
153-
}
154-
true
159+
!default_value.is(&vm.ctx.typing_no_default)
155160
}
156161
}
157162

@@ -408,10 +413,9 @@ pub(crate) mod _typing {
408413
}
409414
}
410415

411-
#[pyattr]
412-
#[pyclass(name = "NoDefault", module = "typing")]
416+
#[pyclass(no_attr, name = "NoDefaultType", module = "typing")]
413417
#[derive(Debug, PyPayload)]
414-
pub(crate) struct NoDefault;
418+
pub struct NoDefault;
415419

416420
#[pyclass(with(Constructor), flags(BASETYPE))]
417421
impl NoDefault {
@@ -429,8 +433,8 @@ pub(crate) mod _typing {
429433
return Err(vm.new_type_error("NoDefaultType takes no arguments".to_owned()));
430434
}
431435

432-
// Return singleton instance
433-
vm.import("typing", 0)?.get_attr("NoDefault", vm)
436+
// Return singleton instance from context
437+
Ok(vm.ctx.typing_no_default.clone().into())
434438
}
435439
}
436440

vm/src/types/zoo.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ pub struct TypeZoo {
8888
pub object_type: &'static Py<PyType>,
8989
pub ellipsis_type: &'static Py<PyType>,
9090
pub none_type: &'static Py<PyType>,
91+
pub typing_no_default_type: &'static Py<PyType>,
9192
pub not_implemented_type: &'static Py<PyType>,
9293
pub generic_alias_type: &'static Py<PyType>,
9394
pub union_type: &'static Py<PyType>,
@@ -179,6 +180,7 @@ impl TypeZoo {
179180
weakproxy_type: weakproxy::PyWeakProxy::init_builtin_type(),
180181
method_descriptor_type: descriptor::PyMethodDescriptor::init_builtin_type(),
181182
none_type: singletons::PyNone::init_builtin_type(),
183+
typing_no_default_type: crate::stdlib::typing::NoDefault::init_builtin_type(),
182184
not_implemented_type: singletons::PyNotImplemented::init_builtin_type(),
183185
generic_alias_type: genericalias::PyGenericAlias::init_builtin_type(),
184186
union_type: union_::PyUnion::init_builtin_type(),

vm/src/vm/context.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ pub struct Context {
4141
pub ellipsis: PyRef<PyEllipsis>,
4242
pub not_implemented: PyRef<PyNotImplemented>,
4343

44+
pub typing_no_default: PyRef<crate::stdlib::typing::NoDefault>,
45+
4446
pub types: TypeZoo,
4547
pub exceptions: exceptions::ExceptionZoo,
4648
pub int_cache_pool: Vec<PyIntRef>,
@@ -278,6 +280,11 @@ impl Context {
278280
let ellipsis = create_object(PyEllipsis, PyEllipsis::static_type());
279281
let not_implemented = create_object(PyNotImplemented, PyNotImplemented::static_type());
280282

283+
let typing_no_default = create_object(
284+
crate::stdlib::typing::NoDefault,
285+
crate::stdlib::typing::NoDefault::static_type(),
286+
);
287+
281288
let int_cache_pool = Self::INT_CACHE_POOL_RANGE
282289
.map(|v| {
283290
PyRef::new_ref(
@@ -317,6 +324,7 @@ impl Context {
317324
true_value,
318325
false_value,
319326
none,
327+
typing_no_default,
320328
empty_tuple,
321329
empty_frozenset,
322330
empty_str,

0 commit comments

Comments
 (0)