Skip to content

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

Closed
@leamingrad

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions