8000 implement more of ParamSpec · RustPython/RustPython@1b3ac3f · GitHub
[go: up one dir, main page]

Skip to content

Commit 1b3ac3f

Browse files
committed
implement more of ParamSpec
1 parent b54ef5b commit 1b3ac3f

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

vm/src/stdlib/typing.rs

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ pub(crate) mod _typing {
8282
#[allow(dead_code)]
8383
pub(crate) struct ParamSpec {
8484
name: PyObjectRef,
85+
bound: Option<PyObjectRef>,
86+
default_value: Option<PyObjectRef>,
87+
evaluate_default: Option<PyObjectRef>,
88+
covariant: bool,
89+
contravariant: bool,
90+
infer_variance: bool,
8591
}
8692

8793
#[pyclass(flags(BASETYPE))]
@@ -90,10 +96,79 @@ pub(crate) mod _typing {
9096
fn name(&self) -> PyObjectRef {
9197
self.name.clone()
9298
}
99+
100+
#[pygetset(magic)]
101+
fn bound(&self, vm: &VirtualMachine) -> PyObjectRef {
102+
if let Some(bound) = self.bound.clone() {
103+
return bound;
104+
}
105+
vm.ctx.none()
106+
}
107+
108+
#[pygetset(magic)]
109+
fn covariant(&self) -> bool {
110+
self.covariant
111+
}
112+
113+
#[pygetset(magic)]
114+
fn contravariant(&self) -> bool {
115+
self.contravariant
116+
}
117+
118+
#[pygetset(magic)]
119+
fn infer_variance(&self) -> bool {
120+
self.infer_variance
121+
}
122+
123+
#[pygetset(magic)]
124+
fn default(&self, vm: &VirtualMachine) -> PyResult {
125+
if let Some(default_value) = self.default_value.clone() {
126+
return Ok(default_value);
127+
}
128+
// handle evaludate_default
129+
if let Some(evaluate_default) = self.evaluate_default.clone() {
130+
let default_value = vm.call_method(evaluate_default.as_ref(), "__call__", ())?;
131+
return Ok(default_value);
132+
}
133+
// TODO: this isn't up to spec
134+
Ok(vm.ctx.none())
135+
}
136+
137+
#[pygetset]
138+
fn evaluate_default(&self, vm: &VirtualMachine) -> PyObjectRef {
139+
if let Some(evaluate_default) = self.evaluate_default.clone() {
140+
return evaluate_default;
141+
}
142+
// TODO: default_value case
143+
vm.ctx.none()
144+
}
145+
146+
#[pymethod(magic)]
147+
fn reduce(&self) -> PyResult {
148+
Ok(self.name.clone())
149+
}
150+
151+
#[pymethod]
152+
fn has_default(&self) -> PyResult<bool> {
153+
// TODO: fix
154+
if self.evaluate_default.is_some() || self.default_value.is_some() {
155+
Ok(true)
156+
} else {
157+
Ok(false)
158+
}
159+
}
93160
}
94161

95162
pub(crate) fn make_paramspec(name: PyObjectRef) -> ParamSpec {
96-
ParamSpec { name }
163+
ParamSpec {
164+
name,
165+
bound: None,
166+
default_value: None,
167+
evaluate_default: None,
168+
covariant: false,
169+
contravariant: false,
170+
infer_variance: false,
171+
}
97172
}
98173

99174
#[pyattr]

0 commit comments

Comments
 (0)
0