Skip to content

Mypy crash when @staticmethod has the same name as an attr #6715

Description

@leamingrad

Summary of the issue:

When using attrs to create a dataclass, adding a static method to the class with the same name as one of the attributes causes mypy to crash.

I am happy to work on a fix here, but would need some guidance on where to start.

Please provide more information to help us understand the issue:

  • Are you reporting a bug, or opening a feature request?

This is a bug - mypy should not crash when running against files.

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.

The following is an minimal reproduction of the issue:

import attr

@attr.s
class MyData:
    is_foo: bool = attr.ib()

    @staticmethod
    def is_foo(string: str) -> bool:
        return False
  • What is the actual behavior/output?

When run against the above file, mypy crashes with the following error output:

$~ mypy test.py --show-traceback
test.py:5: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.701
Traceback (most recent call last):
  File "/home/james/.local/share/virtualenvs/web-findings-dozQMylR/bin/mypy", line 10, in <module>
    sys.exit(console_entry())
  File "mypy/semanal.py", line 3791, in accept
  File "mypy/nodes.py", line 895, in accept__Node_glue
  File "mypy/nodes.py", line 896, in accept
  File "mypy/semanal.py", line 804, in visit_class_def__StatementVisitor_glue
  File "mypy/semanal.py", line 807, in visit_class_def
  File "mypy/semanal.py", line 825, in analyze_class
  File "mypy/semanal.py", line 833, in analyze_class_body_common
  File "mypy/semanal.py", line 895, in apply_class_plugin_hooks
  File "mypy/plugins/attrs.py", line 211, in attr_class_maker_callback
  File "mypy/plugins/attrs.py", line 267, in _analyze_class
AssertionError:
test.py:5: : note: use --pdb to drop into pdb
  • What is the behavior/output you expect?

I'm not 100% what I would expect here. I think that mypy should probably raise an error about the redefinition of something with the same name, as that is what happens if I change the staticmethod to a regular classmethod.

  • What are the versions of mypy and Python you are using?

Python 3.6.8
mypy 0.701

  • Do you see the same issue after installing mypy from Git master?

Yes, although the traceback is a bit more detailed:

test.py:4: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.710+dev.f0c2506eddf2a06e1d830953541b5d431e50073e
test.py:4: : note: use --pdb to drop into pdb
Traceback (most recent call last):
  File "/path/to/my/virtualenv/bin/mypy", line 10, in <module>
    sys.exit(console_entry())
  File "/path/to/my/virtualenv/lib/python3.6/site-packages/mypy/__main__.py", line 7, in console_entry
    main(None)
  File "/path/to/my/virtualenv/lib/python3.6/site-packages/mypy/main.py", line 85, in main
    res = build.build(sources, options, None, flush_errors, fscache)
  File "/path/to/my/virtualenv/lib/python3.6/site-packages/mypy/build.py", line 163, in build
    result = _build(sources, options, alt_lib_path, flush_errors, fscache)
  File "/path/to/my/virtualenv/lib/python3.6/site-packages/mypy/build.py", line 218, in _build
    graph = dispatch(sources, manager)
  File "/path/to/my/virtualenv/lib/python3.6/site-packages/mypy/build.py", line 2461, in dispatch
    process_graph(graph, manager)
  File "/path/to/my/virtualenv/lib/python3.6/site-packages/mypy/build.py", line 2761, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/path/to/my/virtualenv/lib/python3.6/site-packages/mypy/build.py", line 2862, in process_stale_scc
    graph[id].semantic_analysis()
  File "/path/to/my/virtualenv/lib/python3.6/site-packages/mypy/build.py", line 1997, in semantic_analysis
    self.manager.semantic_analyzer.visit_file(self.tree, self.xpath, self.options, patches)
  File "/path/to/my/virtualenv/lib/python3.6/site-packages/mypy/semanal.py", line 309, in visit_file
    self.accept(d)
  File "/path/to/my/virtualenv/lib/python3.6/site-packages/mypy/semanal.py", line 3786, in accept
    node.accept(self)
  File "/path/to/my/virtualenv/lib/python3.6/site-packages/mypy/nodes.py", line 901, in accept
    return visitor.visit_class_def(self)
  File "/path/to/my/virtualenv/lib/python3.6/site-packages/mypy/semanal.py", line 807, in visit_class_def
    self.analyze_class(defn)
  File "/path/to/my/virtualenv/lib/python3.6/site-packages/mypy/semanal.py", line 825, in analyze_class
    self.analyze_class_body_common(defn)
  File "/path/to/my/virtualenv/lib/python3.6/site-packages/mypy/semanal.py", line 833, in analyze_class_body_common
    self.apply_class_plugin_hooks(defn)
  File "/path/to/my/virtualenv/lib/python3.6/site-packages/mypy/semanal.py", line 895, in apply_class_plugin_hooks
    hook(ClassDefContext(defn, decorator, self))
  File "/path/to/my/virtualenv/lib/python3.6/site-packages/mypy/plugins/attrs.py", line 211, in attr_class_maker_callback
    attributes = _analyze_class(ctx, auto_attribs, kw_only)
  File "/path/to/my/virtualenv/lib/python3.6/site-packages/mypy/plugins/attrs.py", line 267, in _analyze_class
    assert isinstance(node, Var)
AssertionError: 
  • What are the mypy flags you are using? (For example --strict-optional)

The local setup.cfg file has the following section:

[mypy]
ignore_missing_imports = True
check_untyped_defs = True

Activity

  1. gvanrossum commented on Apr 24, 2019

    @gvanrossum
    Member

    Looks like a bug in the mypy plugin for attrs, as indicated by the failing assert. I can't help you further except to suggest that you read the code, and run it with print() calls added (or trace it using a debugger like pdb) to figure out how it works.

  2. JelleZijlstra commented on Apr 24, 2019

    @JelleZijlstra
    Member

    Worth noting that the reason the stack trace is nicer on master than in the release is that the release is using a mypyc-compiled binary.

  3. ilevkivskyi commented on Apr 25, 2019

    @ilevkivskyi
    Member

    This is the same as #6493 but with dataclasses -> attrs, the two plugins are very similar, so the existing PR that fixes the other issue can be easily updated to fix also this one, see #6644.

  4. ilevkivskyi commented on Nov 10, 2019

    @ilevkivskyi
    Member

    Note #7918 has an example when this crash appears in another situation (also involving decorators).

  5. added and removed
    topic-pluginsThe plugin API and ideas for new plugins
    on Mar 24, 2022
  6. emmatyping commented on Sep 30, 2022

    @emmatyping
    Member

    I can no longer reproduce the error OP experienced using latest top of tree.

    It now says error: Name "is_foo" already defined on line 5, which is correct.

  7. aedify-swi commented on Nov 25, 2022

    @aedify-swi

    We're still seeing this issue (or rather #7918 / #6493) with 0.991:

    from attrs import frozen
    
    @frozen(kw_only=True)
    class Base:
        name: str
    
    @frozen(kw_only=True)
    class Sub(Base):
        first_name: str
        last_name: str
        
        @property
        def name(self) -> str:
            return f"{self.first_name} {self.last_name}"

    Even if this wasn't allowed, it would be great if mypy gave an error message instead of crashing with no indication where and/or why:

      main()
      File "mypy/main.py", line 95, in main
      File "mypy/main.py", line 174, in run_build
      File "mypy/build.py", line 193, in build
      File "mypy/build.py", line 276, in _build
      File "mypy/build.py", line 2903, in dispatch
      File "mypy/build.py", line 3291, in process_graph
      File "mypy/build.py", line 3386, in process_stale_scc
      File "mypy/semanal_main.py", line 90, in semantic_analysis_for_scc
      File "mypy/semanal_main.py", line 429, in apply_class_plugin_hooks
      File "mypy/semanal_main.py", line 457, in apply_hooks_to_class
      File "mypy/plugins/attrs.py", line 332, in attr_class_maker_callback
      File "mypy/plugins/attrs.py", line 739, in _make_frozen
    AssertionError
    
  8. ilevkivskyi commented on Nov 25, 2022

    @ilevkivskyi
    Member

    Yes, I confirm this crashes on current master.

  9. added a commit that references this issue on Nov 25, 2022
    278a095
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions