Skip to content
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

Calls to super inside pattern-matching classmethods do not work #728

Closed
yggdr opened this issue Apr 15, 2023 · 2 comments
Closed

Calls to super inside pattern-matching classmethods do not work #728

yggdr opened this issue Apr 15, 2023 · 2 comments

Comments

@yggdr
Copy link

yggdr commented Apr 15, 2023

A call to super inside a pattern-matching method that's defined as a classmethod fails with TypeError:

class A:
    @classmethod
    addpattern def method(cls, {'somekey': str()}) = True


class B(A):
    @classmethod
    addpattern def method(cls, {'someotherkey': int(), **rest}) =
        super().method(rest)


B().method({'somekey': 'string', 'someotherkey': 42})
Traceback (most recent call last):
    File "cococlassmethod.py", line 2074, in <module>
      B().method({'somekey': 'string', 'someotherkey': 42})
    File "cococlassmethod.py", line 2070, in method
      return (super().method(rest))
    File "cococlassmethod.py", line 29, in _coconut_super
      return _coconut_py_super(cls, self)
  TypeError: super(type, obj): obj must be an instance or subtype of type

It seems the code that's supposed to make parameterless super() possible on older python versions doesn't handle the pattern-matching part correctly. I looked into this a bit and it seems the part where the super-implementation tries to get the self it fetches some tuple instead.

frame.f_code.co_varnames[0] ==> '_coconut_match_args'
frame.f_locals[frame.f_code.co_varnames[0]] ==> (the-class-I-call-the-super-inside, the-dict-I-pass-into-the-function-to-be-pattern-matched)

What I'm a bit surprised by is that coconut compiles this super even on targets like 3.11 where parameterless super is supported.

@evhub evhub added the bug label Apr 16, 2023
@evhub evhub added this to the v3.0.0 milestone Apr 16, 2023
evhub added a commit that referenced this issue Apr 16, 2023
@evhub
Copy link
Owner

evhub commented Apr 16, 2023

This was a very tricky bug; thanks for finding this! Should be all fixed now on coconut-develop>=3.0.0-a_dev23 (pip install -U coconut-develop to get the fix). Fix should make the above code work on any target, and should make super correctly use the built-in super on targets >= 3.

@evhub evhub closed this as completed Apr 16, 2023
@evhub evhub added the resolved label Apr 16, 2023
@yggdr
Copy link
Author

yggdr commented Apr 16, 2023

... I really did not expect to wake up to you having fixed this already. I'm impressed once again, thanks!

@evhub evhub mentioned this issue May 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants