Skip to content

Ignore mypy lint errors for other files - #15266

Merged
Kartik Raj (karrtikr) merged 9 commits into
microsoft:mainfrom
sbdchd:main
Mar 9, 2021
Merged

Kartik Raj (karrtikr) merged 9 commits into
microsoft:mainfrom
sbdchd:main

Conversation

@sbdchd

@sbdchd Steve Dignam (sbdchd) commented Jan 29, 2021 •

Copy link
Copy Markdown

mypy sometimes returns errors for files that aren't the same as the file
it was called against.

This PR adds some filtering based on the current file name.

Test file structure:

❯ tree foo
foo
├── __init__.py
└── bar.py
# __init__.py
def main() -> None:
    return None
    print("something")
# bar.py
def foo() -> None:
    x: int = "foo"

Before:

Screen Shot 2021-01-28 at 7 49 25 PM

After:
Screen Shot 2021-01-28 at 7 48 33 PM

fixes #10190
related #15216

mypy sometimes returns errors for files that aren't the same as the file
it was called against.

This PR adds some filtering based on the current file name.

fixes microsoft#10190
Comment thread src/client/linters/mypy.ts Outdated
@@ -14,10 +15,19 @@ export class MyPy extends BaseLinter {

protected async runLinter(document: TextDocument, cancellation: CancellationToken): Promise<ILintMessage[]> {
const messages = await this.run([document.uri.fsPath], document, cancellation, REGEX);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option: generate the regex used to extract the message info at runtime with the current file name substituted instead of the file capture group, causing the messages for the other files to be ignored

Comment thread src/client/linters/mypy.ts Outdated

protected async runLinter(document: TextDocument, cancellation: CancellationToken): Promise<ILintMessage[]> {
const messages = await this.run([document.uri.fsPath], document, cancellation, REGEX);
const fileName = document.uri.fsPath.slice(this.getWorkspaceRootPath(document).length + 1);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

original approach was to modify the ILintMessage to include an optional file property and then filter based off that here

regex approach seems less invasive

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also wondering the best way to write a test for this

@karrtikr Kartik Raj (karrtikr) left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Almost there,

Comment thread src/client/linters/mypy.ts Resolved
Comment thread src/test/linters/mypy.unit.test.ts Resolved
Comment thread src/test/linters/mypy.unit.test.ts Resolved

protected async runLinter(document: TextDocument, cancellation: CancellationToken): Promise<ILintMessage[]> {
const messages = await this.run([document.uri.fsPath], document, cancellation, REGEX);
const relativeFilePath = document.uri.fsPath.slice(this.getWorkspaceRootPath(document).length + 1);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

document.uri.fsPath gives us "/Users/foo/workspace-project/foo/bar.py" but we want the path relative to the workspace: foo/bar.py

```
##########Linting Output - mypy##########
foo/__init__.py:4:5: error: Statement is unreachable  [unreachable]
foo/bar.py:2:14: error: Incompatible types in assignment (expression has type "str", variable has type "int")  [assignment]
Found 2 errors in 2 files (checked 1 source file)
```
@codecov-io

Copy link
Copy Markdown

Codecov Report

Merging #15266 (434eded) into main (790c909) will increase coverage by 0%.
The diff coverage is 71%.

@@           Coverage Diff           @@
##            main   #15266    +/-   ##
=======================================
  Coverage     65%      65%            
=======================================
  Files        558      561     +3     
  Lines      26646    26982   +336     
  Branches    3840     3914    +74     
=======================================
+ Hits       17322    17562   +240     
- Misses      8617     8687    +70     
- Partials     707      733    +26     
Impacted Files Coverage Δ
src/client/linters/mypy.ts 41% <50%> (+2%) ⬆️
...nments/discovery/locators/services/condaService.ts 60% <50%> (-25%) ⬇️
...ent/interpreter/autoSelection/rules/winRegistry.ts 93% <100%> (+<1%) ⬆️
src/client/pythonEnvironments/legacyIOC.ts 37% <100%> (+2%) ⬆️
src/client/common/utils/filesystem.ts 27% <0%> (-17%) ⬇️
...sting/common/services/unitTestDiagnosticService.ts 80% <0%> (-16%) ⬇️
...rc/client/pythonEnvironments/common/commonUtils.ts 63% <0%> (-12%) ⬇️
...nments/base/locators/lowLevel/fsWatchingLocator.ts 77% <0%> (-8%) ⬇️
src/client/testing/common/types.ts 93% <0%> (-7%) ⬇️
... and 118 more
@karrtikr

Copy link
Copy Markdown

Thanks for the changes, I'll have a look as soon as possible. FYI #15505 could affect this PR.

@karrtikr

Copy link
Copy Markdown

Steve Dignam (@sbdchd) #15505 just got merged, but it may affect this PR. Can you please rebase off main. (Use git rebase main, and then git push -f to force push the changes here)

@karrtikr Kartik Raj (karrtikr) left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM once the offset is adjusted via rebase

Comment thread src/test/linters/mypy.unit.test.ts Outdated
[lines[2], undefined],
];
for (const [line, expected] of tests) {
const msg = parseLine(line, getRegex('foo/bar.py'), LinterId.MyPy);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Column offset should be passed as 1.

@karrtikr

Copy link
Copy Markdown

Steve Dignam (@sbdchd) The tests seem to be failing, can you please rebase again? The fixes may have landed on main branch.

@sbdchd

Steve Dignam (sbdchd) commented Mar 9, 2021 •

Copy link
Copy Markdown
Author

I think I fixed the tests, I'm having trouble getting the test suite to fail locally

If I throw an exception or expect(true).to.eql(false); in a test, they are still passing, not sure what's up

❯ npm run test:unittests -- --grep='Linting - MyPy'                                                                                                     3.05s

> python@2021.4.0-dev test:unittests /Users/badger/Downloads/vscode-python
> mocha --config ./build/.mocha.unittests.js.json "--grep=Linting - MyPy"



  Linting - MyPy
  ✓ regex
  ✓ regex excludes unexpected files
  ✓ getRegex escapes filename correctly


  3 passing (36ms)
@karrtikr

Copy link
Copy Markdown

Interesting, we generally use expect(true).to.equal(false) syntax instead, I'm not sure about eql. Anyways, if the tests pass on CI this should be good to go.

@karrtikr
Kartik Raj (karrtikr) merged commit 3d4c382 into microsoft:main Mar 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants