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

[mypyc] support of @final types #9612

Closed
sobolevn opened this issue Oct 18, 2020 · 1 comment · Fixed by #17886
Closed

[mypyc] support of @final types #9612

sobolevn opened this issue Oct 18, 2020 · 1 comment · Fixed by #17886
Labels
bug mypy got something wrong topic-final PEP 591 topic-mypyc mypyc bugs

Comments

@sobolevn
Copy link
Member

sobolevn commented Oct 18, 2020

Bug Report

I am trying to compile a module with @final types.
This does not seem possible at the moment with mypyc.

Actual source: https://github.com/dry-python/returns/blob/master/returns/result.py#L312

To Reproduce

from typing_extensions import final


class Base(object):
    """Can be sub-classed."""


@final
class Child(Base):
    """Final instance."""

When mypyc is used on top of this code, this happens:

» mypyc ex.py
ex.py:9: error: Non-extension classes may not inherit from extension classes

As I understand, mypyc treats @final as a regular decorator and then makes Child a non-exntesion class.
And this does not seem correct to me.

Expected Behavior

I would expect this code to compile without any warnings.
And @final should be treated the same way mypy does with regular python code.

  • Mypy version used: 0.790
@jairov4
Copy link
Contributor

jairov4 commented Apr 21, 2024

still happening in 1.0.9
even I think it would be benefical for codegen to apply optimization

JukkaL pushed a commit that referenced this issue Oct 14, 2024
Fixes #9612

This change allows to gain more efficiency where classes are annotated
with `@final` bypassing entirely the vtable for method calls and
property accessors.

For example:
In
```python
@Final
class Vector:
    __slots__ = ("_x", "_y")
    def __init__(self, x: i32, y: i32) -> None:
        self._x = x
        self._y = y

    @Property
    def y(self) -> i32:
        return self._y

def test_vector() -> None:
    v3 = Vector(1, 2)
    assert v3.y == 2
```

The call will produce:

```c
...
cpy_r_r6 = CPyDef_Vector___y(cpy_r_r0);
...
```

Instead of:

```c
...
cpy_r_r1 = CPY_GET_ATTR(cpy_r_r0, CPyType_Vector, 2, farm_rush___engine___vectors2___VectorObject, int32_t); /* y */
...
```
(which uses vtable)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-final PEP 591 topic-mypyc mypyc bugs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants