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

Crash with custom plugin and ErrorCode #15255

Closed
cdce8p opened this issue May 16, 2023 · 0 comments · Fixed by #15327
Closed

Crash with custom plugin and ErrorCode #15255

cdce8p opened this issue May 16, 2023 · 0 comments · Fixed by #15327
Labels
crash topic-plugins The plugin API and ideas for new plugins

Comments

@cdce8p
Copy link
Collaborator

cdce8p commented May 16, 2023

Crash Report
Cross posting it here for better visibility. The original post: #15218 (comment)
The crash / regression (in dev) is caused by the change in #15218.

The example below is with a custom plugin. However this does effect others as well if an error is emitted. I discovered it while testing code with the pydantic mypy plugin.

/CC: @svalentin

Traceback

$ mypy --config-file=custom.ini --show-traceback test.py
test.py:5: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 1.4.0+dev.ce2c918f73fa4e98c9d0398401431fc45b608e29
Traceback (most recent call last):
  File "mypy/checkexpr.py", line 4871, in accept
  File "mypy/checkexpr.py", line 426, in visit_call_expr
  File "mypy/checkexpr.py", line 546, in visit_call_expr_inner
  File "mypy/checkexpr.py", line 1206, in check_call_expr_with_callee_type
  File "mypy/checkexpr.py", line 1289, in check_call
  File "mypy/checkexpr.py", line 1499, in check_callable_call
  File "mypy/checkexpr.py", line 1029, in apply_function_plugin
  File "/.../plugin.py", line 21, in emit_error
    ctx.api.fail("Custom error", ctx.context, code=ERROR)
  File "mypy/checker.py", line 6478, in fail
  File "mypy/messages.py", line 283, in fail
  File "mypy/messages.py", line 258, in report
  File "mypy/errors.py", line 436, in report
  File "mypy/errors.py", line 478, in add_error_info
  File "mypy/errors.py", line 577, in is_ignored_error
  File "mypy/errors.py", line 604, in is_error_code_enabled
AttributeError: attribute 'sub_code_of' of 'ErrorCode' undefined
test.py:9: : note: use --pdb to drop into pdb

To Reproduce

# test.py
def main() -> int:
    return 2

main()

reveal_type(1)
# custom.ini
[mypy]
plugins = plugin.py
# plugin.py
from typing import Callable

from mypy.errorcodes import ErrorCode
from mypy.plugin import FunctionContext, Plugin
from mypy.types import AnyType, Type, TypeOfAny

ERROR = ErrorCode("custom", "", "Custom")


def plugin(version: str) -> type[Plugin]:
    return CustomPlugin


class CustomPlugin(Plugin):
    def get_function_hook(
        self, fullname: str
    ) -> Callable[[FunctionContext], Type] | None:
        if fullname.endswith(".main"):
            return self.emit_error
        return None

    def emit_error(self, ctx: FunctionContext) -> Type:
        ctx.api.fail("Custom error", ctx.context, code=ERROR)
        return AnyType(TypeOfAny.from_error)

Make sure to install the latest compiled version of mypy. E.g. from https://github.com/mypyc/mypy_mypyc-wheels/releases/tag/v1.4.0%2Bdev.ce2c918f73fa4e98c9d0398401431fc45b608e29

mypy --config-file=custom.ini --show-traceback test.py

Your Environment

  • Mypy version used: mypy 1.4.0+dev.ce2c918f73fa4e98c9d0398401431fc45b608e29 (compiled: yes)
  • Python version used: 3.11
@cdce8p cdce8p added the crash label May 16, 2023
@AlexWaygood AlexWaygood added the topic-plugins The plugin API and ideas for new plugins label May 18, 2023
svalentin pushed a commit that referenced this issue May 31, 2023
A class marked with `allow_interpreted_subclasses=True` implicitly
supports serialization while also allowing it to be subclassed by pure
Python code.

Fixes #15255


https://mypyc.readthedocs.io/en/latest/differences_from_python.html#pickling-and-copying-objects
https://mypyc.readthedocs.io/en/latest/native_classes.html#inheritance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash topic-plugins The plugin API and ideas for new plugins
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants