Merge pull request #4035 from TwoPair/union-parameters

Fix making union parameters
This commit is contained in:
Jeong YunWon authored and GitHub committed 2022-08-10 19:14:36 +09:00
commit 73b8e3bb47
2 files changed
+6 -19

No files matched your search

-2
View File
@@ -815,8 +815,6 @@ class UnionTests(unittest.TestCase):
check(x | None, (x, type(None)))
check(None | x, (type(None), x))
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_union_parameter_chaining(self):
T = typing.TypeVar("T")
S = typing.TypeVar("S")
+6 -17
View File
@@ -1,6 +1,6 @@
use super::{genericalias, type_};
use crate::{
builtins::{PyFrozenSet, PyStr, PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef},
builtins::{PyFrozenSet, PyStr, PyStrRef, PyTuple, PyTupleRef, PyType},
class::PyClassImpl,
common::hash,
convert::ToPyObject,
@@ -163,7 +163,7 @@ fn make_parameters(args: &PyTupleRef, vm: &VirtualMachine) -> PyTupleRef {
}
parameters.shrink_to_fit();
PyTuple::new_ref(parameters, &vm.ctx)
dedup_and_flatten_args(PyTuple::new_ref(parameters, &vm.ctx), vm)
}
fn flatten_args(args: PyTupleRef, vm: &VirtualMachine) -> PyTupleRef {
@@ -196,21 +196,10 @@ fn dedup_and_flatten_args(args: PyTupleRef, vm: &VirtualMachine) -> PyTupleRef {
let mut new_args: Vec<PyObjectRef> = Vec::with_capacity(args.len());
for arg in &args {
if !new_args.iter().any(|param| {
match (
PyTypeRef::try_from_object(vm, param.clone()),
PyTypeRef::try_from_object(vm, arg.clone()),
) {
(Ok(a), Ok(b))
if a.is(vm.ctx.types.generic_alias_type)
&& b.is(vm.ctx.types.generic_alias_type) =>
{
param
.rich_compare_bool(arg, PyComparisonOp::Eq, vm)
.ok()
.unwrap()
}
_ => param.is(arg),
}
param
.rich_compare_bool(arg, PyComparisonOp::Eq, vm)
.ok()
.unwrap()
}) {
new_args.push(arg.clone());
}