-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Introduce PyUnionType
#3497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce PyUnionType
#3497
Conversation
@moreal I am sorry to keep this unreviewed for long time. Could you please rebase this PR? |
93d40c7
to
2f14245
Compare
vm/src/builtins/mod.rs
Outdated
pub(crate) mod unionobject; | ||
pub use unionobject::PyUnionObject; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typically we don't name ~object
for RustPython modules and types. pyunion
for module name (to avoid keyword union
) and PyUnion
for the type name would fit to convention.
vm/src/builtins/unionobject.rs
Outdated
PyRef, PyResult, PyValue, TryFromObject, TypeProtocol, VirtualMachine, | ||
}; | ||
use std::fmt; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vm/src/builtins/unionobject.rs
Outdated
|
||
use super::genericalias; | ||
|
||
static CLS_ATTRS: [&str; 1] = ["__module__"]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if there is no specific needs for array, slice also works. (and also const, not to access memory)
static CLS_ATTRS: [&str; 1] = ["__module__"]; | |
const CLS_ATTRS: &[&str] = &["__module__"]; |
vm/src/builtins/unionobject.rs
Outdated
#[pymethod(magic)] | ||
fn repr(&self, vm: &VirtualMachine) -> PyResult<String> { | ||
fn repr_item(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<String> { | ||
if obj.is(&vm.ctx.types.none_type) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if obj.is(&vm.ctx.types.none_type) { | |
if vm.is_none(&obj) { |
This commits make some tests skip with `unittest.skip`: - `test.test_typing.BaseCallableTests.test_pickle()` - `test.test_typing.BaseCallableTests.test_paramspec()` Because `CollectionsCallableTests` and `TypingCallableTests` inherit tests in `BaseCallableTests` but the tests are failed only in `CollectionsCallableTests`. I tried to use `unittest.expectedFailure` but I couldn't find the way to apply it conditionally.
Hello @youknowone, I have worked for making this pull request pass the check at this time and it finished. |
I've faced this many times before, and I have a solution. What you can do is go down to # TODO: RUSTPYTHON, WeirdError: for some reason, this test failed in this class
@unittest.expectedFailure
def test_whatever(self): # TODO: RUSTPYTHON, remove when this passes
super().test_whatever() # TODO: RUSTPYTHON, remove when this passes If you want the answer for your specific case, it's under the cut: Answer for your specific casediff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 330aa1a18..ac97fd781 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -532,8 +532,6 @@ class BaseCallableTests:
alias = Callable[[int, str], float]
self.assertEqual(weakref.ref(alias)(), alias)
- # TODO: RUSTPYTHON
- @unittest.skip("Failed at CollectionsCallableTests")
def test_pickle(self):
Callable = self.Callable
alias = Callable[[int, str], float]
@@ -575,8 +573,6 @@ class BaseCallableTests:
self.assertIs(a().__class__, C1)
self.assertEqual(a().__orig_class__, C1[[int], T])
- # TODO: RUSTPYTHON
- @unittest.skip("Failed at CollectionsCallableTests")
def test_paramspec(self):
Callable = self.Callable
fullname = f"{Callable.__module__}.Callable"
@@ -646,6 +642,15 @@ class TypingCallableTests(BaseCallableTests, BaseTestCase):
class CollectionsCallableTests(BaseCallableTests, BaseTestCase):
Callable = collections.abc.Callable
+ # TODO: RUSTPYTHON
+ @unittest.expectedFailure
+ def test_paramspec(self): # TODO: RUSTPYTHON, remove when this passes
+ super().test_paramspec() # TODO: RUSTPYTHON, remove when this passes
+
+ # TODO: RUSTPYTHON, pickle.PicklingError: Can't pickle <class 'collections.abc._CallableGenericAlias'>: it's not found as collections.abc._CallableGenericAlias
+ @unittest.expectedFailure
+ def test_pickle(self): # TODO: RUSTPYTHON, remove when this passes
+ super().test_pickle() # TODO: RUSTPYTHON, remove when this passes
class LiteralTests(BaseTestCase):
def test_basics(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! I left a few subtle comments.
vm/src/builtins/pyunion.rs
Outdated
|
||
#[pyproperty(magic)] | ||
fn parameters(&self) -> PyObjectRef { | ||
self.parameters.as_object().to_owned() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.parameters.as_object().to_owned() | |
self.parameters.into() |
vm/src/builtins/pyunion.rs
Outdated
|
||
#[pyproperty(magic)] | ||
fn args(&self) -> PyObjectRef { | ||
self.args.as_object().to_owned() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.args.as_object().to_owned() | |
self.args.into() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to apply this suggestion but it failed with the below logs 😿 :
error[E0507]: cannot move out of `self.parameters` which is behind a shared reference
--> vm/src/builtins/pyunion.rs:79:9
|
79 | self.parameters.into()
| ^^^^^^^^^^^^^^^ move occurs because `self.parameters` has type `pyobjectrc::PyRef<PyTuple>`, which does not implement the `Copy` trait
error[E0507]: cannot move out of `self.args` which is behind a shared reference
--> vm/src/builtins/pyunion.rs:84:9
|
84 | self.args.into()
| ^^^^^^^^^ move occurs because `self.args` has type `pyobjectrc::PyRef<PyTuple>`, which does not implement the `Copy` trait
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.args.as_object().to_owned() | |
self.args.clone().into() |
Adding a clone
will work.
Co-authored-by: Jeong YunWon <69878+youknowone@users.noreply.github.com>
Co-authored-by: Jeong YunWon <69878+youknowone@users.noreply.github.com>
95b06ee
to
dc968c2
Compare
ddcc922
to
d2e3bff
Compare
Thank you for long time work! |
I also appreciate your help and passion! 🙇🏻♂️ |
This pull request is related with #3419, #3475.
This pull request does:
PyUnionType
(a.k.a.types.UnionType
,unionobject
in CPython)_py_abc
3.10.0abc
3.10.0types
3.10.0typing
3.10.0_collections_abc
3.10.2