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

Wrong reportGeneralTypeIssues issued for attrs code with kw_only and overloading #4670

Closed
alonme opened this issue Jul 31, 2023 · 2 comments
Closed
Assignees

Comments

@alonme
Copy link

alonme commented Jul 31, 2023

Environment data

  • Language Server version: Pylance language server 2023.7.40
  • OS and version: MacOS 13.0.1 (22A400)
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.8.7

Code Snippet

import attrs # attrs==22.2.0


@attrs.define
class A:
    x: int = attrs.field(kw_only=True, default=5)
    y: int = attrs.field()


a = A(3)


@attrs.define
class B(A):
    z: int = attrs.field()
    x: int = attrs.field(default=20)


b = B(3, 4)

print(b.x, b.y, b.z) # 20 3 4

Repro Steps

  1. By running the attached script you can see how attrs actually works with these kind of classes, and by pasting the same code into VSCode - you will see that pylance flags the line b = B(3,4) as if it has an issue

Expected behavior

Pylance shouldn't flag this kind of code

Actual behavior

Pylance shows the following

Argument missing for parameter "z"Pylance[reportGeneralTypeIssues]

Logs

2023-07-31 14:29:04.295 [info] [Info  - 2:29:04 PM] (87133) SourceFile: Received fs event 'change' for path '/Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py'
2023-07-31 14:29:04.295 [info] (87133) Background analysis message: markFilesDirty
2023-07-31 14:29:04.316 [info] (87133) Background analysis message: analyze
2023-07-31 14:29:14.558 [info] [Info  - 2:29:14 PM] (87133) SourceFile: Received fs event 'change' for path '/Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py'
2023-07-31 14:29:14.558 [info] (87133) Background analysis message: markFilesDirty
2023-07-31 14:29:14.559 [info] (87133) Background analysis message: analyze
2023-07-31 14:29:16.196 [info] (87133) Background analysis message: getDiagnosticsForRange
2023-07-31 14:29:16.196 [info] (87133) Background analysis message: getDiagnosticsForRange
2023-07-31 14:29:16.939 [info] (87133) Background analysis message: setFileOpened
2023-07-31 14:29:16.939 [info] (87133) Background analysis message: markFilesDirty
2023-07-31 14:29:16.949 [info] (87133) [FG] parsing: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (1ms)
2023-07-31 14:29:16.950 [info] (87133) [FG] binding: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (1ms)
2023-07-31 14:29:17.189 [info] (87133) Background analysis message: analyze
2023-07-31 14:29:17.198 [info] (87133) [BG(1)] analyzing: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py ...
2023-07-31 14:29:17.199 [info] (87133) [BG(1)]   parsing: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (1ms)
2023-07-31 14:29:17.200 [info] (87133) [BG(1)]   binding: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (0ms)
2023-07-31 14:29:17.245 [info] (87133) [BG(1)]   checking: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (55ms)
2023-07-31 14:29:17.245 [info] (87133) [BG(1)] analyzing: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (56ms)
2023-07-31 14:29:17.247 [info] (87133) Background analysis message: resumeAnalysis
2023-07-31 14:29:17.400 [info] (87133) Background analysis message: getSemanticTokens delta
2023-07-31 14:29:17.405 [info] (87133) [BG(1)] getSemanticTokens delta previousResultId:1690802937540 at /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (4ms)
2023-07-31 14:29:17.664 [info] (87133) Background analysis message: setFileOpened
2023-07-31 14:29:17.664 [info] (87133) Background analysis message: markFilesDirty
2023-07-31 14:29:17.830 [info] [Info  - 2:29:17 PM] (87133) SourceFile: Received fs event 'add' for path '/Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py.92e567934636932fec57eb9f85e756ba.tmp'
2023-07-31 14:29:17.831 [info] [Info  - 2:29:17 PM] (87133) SourceFile: Received fs event 'change' for path '/Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py.92e567934636932fec57eb9f85e756ba.tmp'
2023-07-31 14:29:17.914 [info] (87133) Background analysis message: analyze
2023-07-31 14:29:17.915 [info] (87133) [BG(1)] analyzing: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py ...
2023-07-31 14:29:17.915 [info] (87133) [BG(1)]   parsing: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (0ms)
2023-07-31 14:29:17.916 [info] (87133) [BG(1)]   binding: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (0ms)
2023-07-31 14:29:17.970 [info] (87133) [FG] parsing: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (3ms)
2023-07-31 14:29:17.972 [info] (87133) [FG] binding: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (1ms)
2023-07-31 14:29:17.981 [info] (87133) [BG(1)]   checking: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (65ms)
2023-07-31 14:29:17.982 [info] (87133) [BG(1)] analyzing: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (66ms)
2023-07-31 14:29:17.983 [info] (87133) Background analysis message: resumeAnalysis
2023-07-31 14:29:18.090 [info] [Info  - 2:29:18 PM] (87133) SourceFile: Received fs event 'change' for path '/Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py'
2023-07-31 14:29:18.091 [info] [Info  - 2:29:18 PM] (87133) SourceFile: Received fs event 'change' for path '/Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py.92e567934636932fec57eb9f85e756ba.tmp'
2023-07-31 14:29:18.091 [info] (87133) Background analysis message: markFilesDirty
2023-07-31 14:29:18.146 [info] (87133) Background analysis message: analyze
2023-07-31 14:29:18.150 [info] (87133) Background analysis message: getSemanticTokens delta
2023-07-31 14:29:18.150 [info] (87133) [BG(1)] getSemanticTokens delta previousResultId:1690802957399 at /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (3ms)
2023-07-31 14:29:18.238 [info] (87133) Background analysis message: getDiagnosticsForRange
2023-07-31 14:29:18.238 [info] (87133) Background analysis message: getDiagnosticsForRange
2023-07-31 14:29:18.917 [info] (87133) Background inlayHint message
2023-07-31 14:29:18.917 [info] (87133) [BG(1)] getInlayHints range 0:0 - 22:0 at /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (1ms)
2023-07-31 14:29:29.654 [info] (87133) Background analysis message: setFileOpened
2023-07-31 14:29:29.654 [info] (87133) Background analysis message: markFilesDirty
2023-07-31 14:29:29.905 [info] (87133) Background analysis message: analyze
2023-07-31 14:29:29.907 [info] (87133) [BG(1)] analyzing: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py ...
2023-07-31 14:29:29.908 [info] (87133) [BG(1)]   parsing: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (1ms)
2023-07-31 14:29:29.911 [info] (87133) [BG(1)]   binding: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (0ms)
2023-07-31 14:29:29.952 [info] (87133) [BG(1)]   checking: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (43ms)
2023-07-31 14:29:29.953 [info] (87133) [BG(1)] analyzing: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (44ms)
2023-07-31 14:29:29.954 [info] (87133) Background analysis message: getDiagnosticsForRange
2023-07-31 14:29:29.955 [info] (87133) [FG] parsing: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (1ms)
2023-07-31 14:29:29.956 [info] (87133) [FG] binding: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (1ms)
2023-07-31 14:29:29.957 [info] (87133) Background analysis message: resumeAnalysis
2023-07-31 14:29:29.959 [info] (87133) Background analysis message: getDiagnosticsForRange
2023-07-31 14:29:30.105 [info] (87133) Background analysis message: getSemanticTokens delta
2023-07-31 14:29:30.108 [info] (87133) [BG(1)] getSemanticTokens delta previousResultId:1690802958138 at /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (3ms)
2023-07-31 14:29:30.127 [info] (87133) Background analysis message: setFileOpened
2023-07-31 14:29:30.127 [info] (87133) Background analysis message: markFilesDirty
2023-07-31 14:29:30.210 [info] (87133) Background analysis message: getDiagnosticsForRange
2023-07-31 14:29:30.211 [info] (87133) [FG] parsing: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (1ms)
2023-07-31 14:29:30.211 [info] (87133) [FG] binding: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (0ms)
2023-07-31 14:29:30.212 [info] (87133) Background analysis message: getDiagnosticsForRange
2023-07-31 14:29:30.263 [info] [Info  - 2:29:30 PM] (87133) SourceFile: Received fs event 'add' for path '/Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py.92e567934636932fec57eb9f85e756ba.tmp'
2023-07-31 14:29:30.263 [info] [Info  - 2:29:30 PM] (87133) SourceFile: Received fs event 'change' for path '/Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py.92e567934636932fec57eb9f85e756ba.tmp'
2023-07-31 14:29:30.459 [info] (87133) Background analysis message: analyze
2023-07-31 14:29:30.461 [info] (87133) [BG(1)] analyzing: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py ...
2023-07-31 14:29:30.463 [info] (87133) [BG(1)]   parsing: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (1ms)
2023-07-31 14:29:30.464 [info] (87133) [BG(1)]   binding: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (0ms)
2023-07-31 14:29:30.504 [info] (87133) [BG(1)]   checking: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (43ms)
2023-07-31 14:29:30.504 [info] (87133) [BG(1)] analyzing: /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (44ms)
2023-07-31 14:29:30.505 [info] (87133) Background analysis message: resumeAnalysis
2023-07-31 14:29:30.551 [info] [Info  - 2:29:30 PM] (87133) SourceFile: Received fs event 'change' for path '/Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py'
2023-07-31 14:29:30.551 [info] [Info  - 2:29:30 PM] (87133) SourceFile: Received fs event 'change' for path '/Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py.92e567934636932fec57eb9f85e756ba.tmp'
2023-07-31 14:29:30.552 [info] (87133) Background analysis message: markFilesDirty
2023-07-31 14:29:30.572 [info] (87133) Background analysis message: analyze
2023-07-31 14:29:30.615 [info] (87133) Background analysis message: getSemanticTokens delta
2023-07-31 14:29:30.616 [info] (87133) [BG(1)] getSemanticTokens delta previousResultId:1690802970105 at /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (2ms)
2023-07-31 14:29:30.759 [info] (87133) Background analysis message: getDiagnosticsForRange
2023-07-31 14:29:30.759 [info] (87133) Background analysis message: getDiagnosticsForRange
2023-07-31 14:29:31.378 [info] (87133) Background inlayHint message
2023-07-31 14:29:31.379 [info] (87133) [BG(1)] getInlayHints range 0:0 - 22:0 at /Users/alonmenczer/dev/blaaa/.vscode/pylance_issue.py (0ms)

@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label Jul 31, 2023
@ni-todo-spot
Copy link

This would help me also!
very annoying 😞

@erictraut
Copy link
Contributor

Attrs uses dataclass_transform (documented in PEP 681) to describe its behavior to static type checkers. The dataclass_transform decorator indicates that the class (in this case, attrs.define) has the same behaviors and semantics as the stdlib dataclass. For the most part, attrs.define does act consistently with dataclass. However, it is deviating from dataclass in how it treats kw_only in an field override. I don't know if this is intentional or an unintended oversight (i.e. a bug) in attrs. You may want to report it to the maintainers of attrs and see if they're willing to fix it so it's consistent with dataclass.

In any case, to make this work with pylance and other static analysis tools, you will need to modify your code slightly. In particular, you need to repeat the kw_only=True in the override of field x within class B. This modified code works with with both dataclass and attrs.define.

    x: int = field(kw_only=True, default=20)

@debonte debonte closed this as completed Jul 31, 2023
@debonte debonte added external bug and removed needs repro Issue has not been reproduced yet labels Jul 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants