Skip to content

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

Merged
merged 31 commits into from
Feb 6, 2022
Merged

Introduce PyUnionType #3497

merged 31 commits into from
Feb 6, 2022

Conversation

moreal
Copy link
Contributor

@moreal moreal commented Dec 5, 2021

This pull request is related with #3419, #3475.

This pull request does:

  • Introduce PyUnionType (a.k.a. types.UnionType, unionobject in CPython)
  • Bump modules
    • _py_abc 3.10.0
    • abc 3.10.0
    • types 3.10.0
    • typing 3.10.0
    • _collections_abc 3.10.2

@youknowone
Copy link
Member

@moreal I am sorry to keep this unreviewed for long time. Could you please rebase this PR?

@moreal moreal marked this pull request as ready for review January 23, 2022 11:40
@moreal moreal changed the title Introduce PyUnionType WIP: Introduce PyUnionType Jan 24, 2022
Comment on lines 85 to 86
pub(crate) mod unionobject;
pub use unionobject::PyUnionObject;
Copy link
Member

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.

PyRef, PyResult, PyValue, TryFromObject, TypeProtocol, VirtualMachine,
};
use std::fmt;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change


use super::genericalias;

static CLS_ATTRS: [&str; 1] = ["__module__"];
Copy link
Member

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)

Suggested change
static CLS_ATTRS: [&str; 1] = ["__module__"];
const CLS_ATTRS: &[&str] = &["__module__"];

#[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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if obj.is(&vm.ctx.types.none_type) {
if vm.is_none(&obj) {

moreal added 10 commits January 26, 2022 01:48
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.
@moreal moreal changed the title WIP: Introduce PyUnionType Introduce PyUnionType Jan 29, 2022
@moreal
Copy link
Contributor Author

moreal commented Jan 29, 2022

@moreal I am sorry to keep this unreviewed for long time. Could you please rebase this PR?

Hello @youknowone, I have worked for making this pull request pass the check at this time and it finished.
I have a question about the 9fd840d commit. As I wrote on the commit message, there were two failed tests but the tests are in the BaseCallableTests and I couldn't apply unittest.expectedFailure conditionally. So I applied unittest.skip with # TODO: RUSTPYTHON. What do you think about it? That seems not good at the side to skip also two passing tests, not two failed tests.

@fanninpm
Copy link
Contributor

I've faced this many times before, and I have a solution. What you can do is go down to CollectionsCallableTests and do something like this:

    # 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 case
diff --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):

@moreal
Copy link
Contributor Author

moreal commented Jan 30, 2022

@fanninpm I didn't think it works like that 😓 Thank you for your tip and I applied it in a90d18e commit! 🙏🏻

@moreal moreal requested a review from youknowone January 30, 2022 13:53
Copy link
Member

@youknowone youknowone left a 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.


#[pyproperty(magic)]
fn parameters(&self) -> PyObjectRef {
self.parameters.as_object().to_owned()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.parameters.as_object().to_owned()
self.parameters.into()


#[pyproperty(magic)]
fn args(&self) -> PyObjectRef {
self.args.as_object().to_owned()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.args.as_object().to_owned()
self.args.into()

Copy link
Contributor Author

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

Copy link
Member

@youknowone youknowone Feb 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.args.as_object().to_owned()
self.args.clone().into()

Adding a clone will work.

moreal and others added 4 commits January 31, 2022 21:55
Co-authored-by: Jeong YunWon <69878+youknowone@users.noreply.github.com>
Co-authored-by: Jeong YunWon <69878+youknowone@users.noreply.github.com>
@moreal moreal requested a review from youknowone January 31, 2022 15:45
@youknowone youknowone merged commit dded3a6 into RustPython:main Feb 6, 2022
@youknowone
Copy link
Member

Thank you for long time work!

@moreal
Copy link
Contributor Author

moreal commented Feb 6, 2022

Thank you for long time work!

I also appreciate your help and passion! 🙇🏻‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants