KEMBAR78
mypy plugin "get_attribute_hook" called for assignment · Issue #17878 · python/mypy · GitHub
Skip to content

mypy plugin "get_attribute_hook" called for assignment #17878

@AnjoMan

Description

@AnjoMan

Bug Report

I encountered unexpected behaviour while trying to develop a mypy extension: one of the plugin hooks is behaving in a way i did not expect, and which the documentation says it shouldn't.

To Reproduce

Create a directory with the following files:

  1. A mypy plugin defining a get_attribute_hook
# mypy_plugin.py
from typing import Type
import mypy.plugin


def plugin(version: str):
    return CustomPlugin


class CustomPlugin(mypy.plugin.Plugin):
    def get_attribute_hook(self, fullname: str):
        if "CheckedClass" in fullname:
            return attr_access_hook
        return None


def attr_access_hook(ctx: mypy.plugin.AttributeContext) -> Type:
    ctx.api.fail(f"You shouldn't access attrs of CheckedClass", ctx.context)
    return ctx.default_attr_type  # type: ignore
  1. Entry in pyproject.toml to have mypy integrate with the plugin
# pyproject.toml
[tool.mypy]
plugins = "mypy_plugin.py"
  1. test code:
# attr_access.py
class CheckedClass:
    name: int

obj_1 = CheckedClass()
obj_1.name = "sir phancalot"
name = obj_1.name  # `get_attribute_hook` should be called for this

I should then run mypy on the test code using the command $ mypy attr_acces.py

Expected Behavior

I expect two mypy errors:

  • once for line 6 of attr_access.py, where i assign a string to an attribute that was declared as int
  • once for line 7 of attr_access.py, where my code accesses the 'name' attribute of an instance of CheckedClass, triggering my hook

Actual Behavior

I get three errors

  • once for the incorrect assignment
  • once for line 7 where the code triggers my hook
  • additionally for line 6 where i assign to the attribute.

This indicates that get_attribute_hook is called for attribute assignment, in addition to attribute access. This is surprising to me and runs counter to what the documentation on this function says: https://mypy.readthedocs.io/en/stable/extending_mypy.html#current-list-of-plugin-hooks

here is my console output when i run mypy against this file:

$ mypy attr_access.py
attr_access.py:6: error: You shouldn't access attrs of CheckedClass  [misc]
attr_access.py:6: error: Incompatible types in assignment (expression has type "str", variable has type "int")  [assignment]
attr_access.py:7: error: You shouldn't access attrs of CheckedClass  [misc]
Found 3 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.11.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): see reproduce steps
  • Python version used: 3.12.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongdocumentationtopic-pluginsThe plugin API and ideas for new plugins

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions