Skip to content

Passing dataclass default_factory as positional argument causes internal error. #10248

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

Closed
jonathanslenders opened this issue Mar 26, 2021 · 7 comments · Fixed by #11180
Closed
Labels

Comments

@jonathanslenders
Copy link

Crash Report

The detaclasses field is missing a default_factory keyword argument. I'm passing it as a positional argument.

Traceback

Traceback (most recent call last):
  File "/home/jonathan/.pyenv/versions/lazy-maestro-3.9/bin/mypy", line 33, in <module>
    sys.exit(load_entry_point('mypy', 'console_scripts', 'mypy')())
  File "/home/jonathan/git/mypy/mypy/__main__.py", line 11, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "/home/jonathan/git/mypy/mypy/main.py", line 98, in main
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/home/jonathan/git/mypy/mypy/build.py", line 179, in build
    result = _build(
  File "/home/jonathan/git/mypy/mypy/build.py", line 253, in _build
    graph = dispatch(sources, manager, stdout)
  File "/home/jonathan/git/mypy/mypy/build.py", line 2688, in dispatch
    process_graph(graph, manager)
  File "/home/jonathan/git/mypy/mypy/build.py", line 3012, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/home/jonathan/git/mypy/mypy/build.py", line 3104, in process_stale_scc
    mypy.semanal_main.semantic_analysis_for_scc(graph, scc, manager.errors)
  File "/home/jonathan/git/mypy/mypy/semanal_main.py", line 78, in semantic_analysis_for_scc
    process_top_levels(graph, scc, patches)
  File "/home/jonathan/git/mypy/mypy/semanal_main.py", line 199, in process_top_levels
    deferred, incomplete, progress = semantic_analyze_target(next_id, state,
  File "/home/jonathan/git/mypy/mypy/semanal_main.py", line 326, in semantic_analyze_target
    analyzer.refresh_partial(refresh_node,
  File "/home/jonathan/git/mypy/mypy/semanal.py", line 394, in refresh_partial
    self.refresh_top_level(node)
  File "/home/jonathan/git/mypy/mypy/semanal.py", line 405, in refresh_top_level
    self.accept(d)
  File "/home/jonathan/git/mypy/mypy/semanal.py", line 4835, in accept
    node.accept(self)
  File "/home/jonathan/git/mypy/mypy/nodes.py", line 950, in accept
    return visitor.visit_class_def(self)
  File "/home/jonathan/git/mypy/mypy/semanal.py", line 1048, in visit_class_def
    self.analyze_class(defn)
  File "/home/jonathan/git/mypy/mypy/semanal.py", line 1125, in analyze_class
    self.analyze_class_body_common(defn)
  File "/home/jonathan/git/mypy/mypy/semanal.py", line 1134, in analyze_class_body_common
    self.apply_class_plugin_hooks(defn)
  File "/home/jonathan/git/mypy/mypy/semanal.py", line 1180, in apply_class_plugin_hooks
    hook(ClassDefContext(defn, decorator, self))
  File "/home/jonathan/git/mypy/mypy/plugins/dataclasses.py", line 361, in dataclass_class_maker_callback
    transformer.transform()
  File "/home/jonathan/git/mypy/mypy/plugins/dataclasses.py", line 100, in transform
    attributes = self.collect_attributes()
  File "/home/jonathan/git/mypy/mypy/plugins/dataclasses.py", line 249, in collect_attributes
    has_field_call, field_args = _collect_field_args(stmt.rvalue)
  File "/home/jonathan/git/mypy/mypy/plugins/dataclasses.py", line 378, in _collect_field_args
    assert name is not None.)

To Reproduce

from dataclasses import dataclass, field

@dataclass
class C:
    attribute: int = field(int)

Your Environment

  • Mypy version used: mypy 0.820+dev.797544d8f97c478770eb178ba966cc7b1d0e6020.dirty
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): /
  • Python version used: 3.9.2
  • Operating system and version: Linux
@syheliel
Copy link

syheliel commented Apr 1, 2021

The definition of field disable the positional arguments, see PEP-3102:

def field(*,
          default: _MISSING_TYPE = MISSING,
          default_factory: _MISSING_TYPE = MISSING,
          init: bool = True,
          repr: bool = True,
          hash: Any = None,
          compare: bool = True,
          metadata: Any = None) -> Field

If you want to use default_factory, you must specify it as the keyword.

@jonathanslenders
Copy link
Author

@syheliel: Of course, but forgetting to do so should not crash Mypy. That's what this issue is about.

@syheliel
Copy link

syheliel commented Apr 3, 2021

@jonathanslenders: I tried the following codes and doesn't cause the crash. It seems that when using decorator @dataclass, check_overload_call are not called, which leads to wrong argument's underreporting and causes the crash.

from dataclasses import dataclass, field
class A:
    a:int = field(int)
a = field(int)

result:

example.py:9: error: No overload variant of "field" matches argument type "Type[int]"
example.py:9: note: Possible overload variants:
example.py:9: note:     def [_T] field(*, default: _T, init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., metadata: Optional[Mapping[str, Any]] = ...) -> _T
example.py:9: note:     def [_T] field(*, default_factory: Callable[[], _T], init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., metadata: Optional[Mapping[str, Any]] = ...) -> _T
example.py:9: note:     def field(*, init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., metadata: Optional[Mapping[str, Any]] = ...) -> Any
example.py:10: error: No overload variant of "field" matches argument type "Type[int]"
example.py:10: note: Possible overload variants:
example.py:10: note:     def [_T] field(*, default: _T, init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., metadata: Optional[Mapping[str, Any]] = ...) -> _T
example.py:10: note:     def [_T] field(*, default_factory: Callable[[], _T], init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., metadata: Optional[Mapping[str, Any]] = ...) -> _T
example.py:10: note:     def field(*, init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., metadata: Optional[Mapping[str, Any]] = ...) -> Any
Found 2 errors in 1 file (checked 1 source file)

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Sep 21, 2021

I was investigating this issue and I just noticed that this crash is also "fixed" by the latest PR #11137, but the error message looks a bit odd now.

Here is an example as a unit test:

[case testDataclassFieldDoesNotFailOnPositionalArguments]
# flags: --python-version 3.7
from dataclasses import dataclass, field

@dataclass
class C:
    x: int = field(0)  # E: Unpacking **kwargs in "field()" is not supported \
                       # E: No overload variant of "field" matches argument type "int" \
                       # N: Possible overload variants: \
                       # N:     def [_T] field(*, default: _T, init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., metadata: Optional[Mapping[str, Any]] = ..., kw_only: bool = ...) -> _T \
                       # N:     def [_T] field(*, default_factory: Callable[[], _T], init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., metadata: Optional[Mapping[str, Any]] = ..., kw_only: bool = ...) -> _T \
                       # N:     def field(*, init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., metadata: Optional[Mapping[str, Any]] = ..., kw_only: bool = ...) -> Any
[builtins fixtures/dataclasses.pyi]

This is an obvious type error anyway, so maybe mypy devs don't have to really care about this kind of issue though.

@JelleZijlstra
Copy link
Member

Thanks for pointing this out! It's still useful to make the error as clear as possible. cc @sobolevn in case you're interested in tweaking this code a bit more.

@sobolevn
Copy link
Member

Yes, sure. Any suggestions for a better message? 🤔

@JelleZijlstra
Copy link
Member

"field()" does not accept positional arguments maybe?

JukkaL pushed a commit that referenced this issue Sep 24, 2021
)

Fixes #10248

The crash due to dataclasses.field was fixed in the recent PR #11137, but the error 
message was wrong when positional arguments are used.

I update the error message based on 
#10248 (comment)
(Thanks @JelleZijlstra!)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants