Skip to content

Ignore specific lines #500

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
Shookit opened this issue Nov 18, 2014 · 11 comments
Closed

Ignore specific lines #500

Shookit opened this issue Nov 18, 2014 · 11 comments

Comments

@Shookit
Copy link

Shookit commented Nov 18, 2014

I don't know if this is intentionally omitted, or if I'm just not aware of the functionality, but is there any way to ignore mypy errors line-by-line?

For instance, one of the files I'm using mypy on works, but for certain lines...

graph.py, line 3: No module named 'numpy'
graph.py, line 4: No module named 'matplotlib.pyplot'
graph.py, line 5: No module named 'scipy.stats'
graph.py, line 155: Unexpected keyword argument "key" for "max"

...mypy gives errors incorrectly that I'd like to ignore (the modules do exist, and max does take the 'key' argument).

Is there any way to skip these lines, such as in a linter, or a way to hint to mypy that these are valid?

@rockneurotiko
Copy link
Contributor

Check this issue #486 , right now you can't, but there are some things to do before to allow that.

@JukkaL
Copy link
Collaborator

JukkaL commented Nov 21, 2014

This is something that I'd like to have and have thought about it a little. A potential approach is to use a special comment for this:

import numpy  # mypy: ignore

We could also support an if statement for this (this actually partially implemented already but completely undocumented):

from typing import MYPY

if not MYPY:
    import numpy

The value of MYPY would always be False at runtime, but when type checking it would be considered as a compile-time constant with value True that could be used for a kind of conditional compilation -- a if not MYPY block would be ignored by the type checker (but any newly defined names would be given the type Any, perhaps).

Or maybe we can support using a comment to ignore multiple lines:

# mypy: begin ignore
import numpy
import matplotlib.pyplot
import scipy.stats
# mypy: end ignore

Implementation of #486 would be awesome but it does not help when there are C extension modules. However, we may be able to automatically generate pure dynamically typed stubs for C extension modules automatically. @ashleyh started writing a stub generator (I have a clone at https://github.com/JukkaL/mypy-stubgen) but there hasn't been updates in a long time.

@spkersten
Copy link
Contributor

I like annotations to instruct mypy to ignore something much more than having to change code by adding if not MYPY guards.

@JukkaL
Copy link
Collaborator

JukkaL commented Mar 26, 2015

PEP 484 uses # type: ignore for ignoring type errors on particular lines, and mypy should support that. Also, using # type: ignore close to the top of a file should skip checking that file altogether.

@JukkaL JukkaL closed this as completed in c0334e4 Mar 29, 2015
@JukkaL
Copy link
Collaborator

JukkaL commented Mar 29, 2015

Okay, now you can use # type: ignore to silence some errors. Example:

import foo # type: ignore
foo.f()  # okay

tomv564 pushed a commit to sublimelsp/LSP that referenced this issue Dec 31, 2018
* Add a bulb

* Delete unneeded import and switch from absolute to relative import

* Fire request on selection modified, on TextCommand and after Did Change event

Request needs to be send on selection modified, to determine whether to show the bulb or not by the caret position.

The request needs to be send when the TextCommand is run, because the command might be triggered from the phantom (error|warning|hint) pop-up and when it is triggered the caret might already be on a place where code action exist, so the mouse will trigger those code actions instead of the ones that were suppose to be triggered from the popup.

And finally the request needs to be send after the `textDocument/didChange`. So the code actions show suggestions for the current version of document and not for an outdated one.

* Introduce cache

Code actions get cached on each request, so when running the TextCommand the menu will be populated my cache.

However, hovers for warning, errors, hint can run the code action text command. In that case we need to fire a new request, by passing a flag `make_request`, so the code actions that will be displayed are related to the point from where the hover was triggered.

* Fix formating

* add icons

* format import

* Don't show the command in the right click menu, when 0 code actions

This is different from is_enabled because I don't want to disable the command from running, so the user get the `no code actions` popup

* Fix formating

* Remove return type

* Ignore type, python/mypy#500 (comment)

Don't know how to typehint debounce.t

* return None for __init__

* leave hover unchanged

* Remove cache and event listener, Add a command to update code actions

* Need to be at least 500ms

To avoid messages in status bar like `Request textDocument/codeAction failed with message. Debug failure. Expected 80 <= 50`

* Don't fire command in windows.py file

Instead, listen in code_actions.py on_modified_async.

An explanation.
The following will explain why we need to listen on changes?

Short Explanation
So we don't end up with a bulb in the gutter when there are no code actions.

Long (Detailed) explanation
1. When on_modified_async is fired, on_selection_modified_async is fired as well.

2. When inserting text,
I need to make sure that textDocument/codeAction is fired after textDocument/didChange

Because of this line in windows.py, notice the 0.5s delay.
```python
self._sublime.set_timeout_async(lambda: self.purge_did_change(buffer_id, buffer_version), 500)
```
source: https://github.com/tomv564/LSP/blob/41eba6ef3ca1db9fb2ab4f3b393afe4e376b4c40/plugin/core/windows.py#L256

That is the reason the handle_modified_async fire_requsest has a debounce of 0.8s,
0.3s after from textDocument/didChange.

3. If handle_modified_async is fired,
it will debounce fire_request call for another 0.3s (0.8s of handle_modified_async - 0.5s of fire_request)

It is needed to check if the code actions are valid after we made some changes.
So we don't end up with a bulb in the gutter when there are no code actions

* Add a flag

* Leave windows.py unchanged

* Rename show_bulb to show_code_actions_bulb

* Hide bulb when  on_selection_modified_async is fired and remove on_modified_async listener.

For that change I needed to increase the debounce for fire_request from 0.5 to 0.8 ().

Removed LspUpdateCodeActionsCommand, no need for it

Pros:
Simpler code, with not so noticeable changes for the user
Cons:
Fires 300ms slower for on_selection_modified_async, that it used to do, but not so noticeable as said

* Reintroduce on_modified_async, because of a better user experience

* Revert "Reintroduce on_modified_async, because of a better user experience"

This reverts commit ccef8b7.
@CMCDragonkai
Copy link

If the expression being ignored is spread out across lines, the ignore annotation doesn't work.

    with subprocess.Popen(
            *args, bufsize=1, stderr=subprocess.PIPE, **kwargs) as proc: # type: ignore

@ilevkivskyi
Copy link
Member

If the expression being ignored is spread out across lines, the ignore annotation doesn't work.

This is supported only on Python 3.8, on older versions you need to place the # type: ignore comment exactly on the line where error appears.

@and-semakin
Copy link

It would be nice to ignore code blocks like this, because with use of black it gets difficult to correctly place single type: ignore comment, for example:

def some_func(long_argument_name, another_long_argument_name) -> None:  # type: ignore
    pass

Now run black to format this code:

def some_func(
    long_argument_name,
    another_long_argument_name
) -> None:  # type: ignore
    pass

black has moved type: ignore comment on another line and now mypy fails. So after auto-formatting I have to manually replace the comment to the same line where def is located. Despite the fact that it looks like a bug in black, it would be convenient to have a way to disable type checking at once in the whole code block:

# type: begin ignore
def some_func(
    long_argument_name,
    another_long_argument_name
) -> None:
    # type: end ignore
    print("Now code formatter can't break my shitty code anymore!")

@MattOates
Copy link

MattOates commented Jun 10, 2019

@and-semakin this can also be resolved if like the pylint: disable directive you can just put a line comment by itself the line before to ignore the next line. Less likely to cause havoc than an open/close type semantics. What happens if someone moves some code or deletes the end ignore? etc.

stephenfin added a commit to getpatchwork/git-pw that referenced this issue Dec 8, 2019
Turns out multi-line ignores aren't a thing until Python 3.8 [1]. I've
no idea how this was passing beforehand. The fix is easy - change the
order of the args, allowing us to place things where they need to go.

[1] python/mypy#500 (comment)

Signed-off-by: Stephen Finucane <[email protected]>
@tueda
Copy link

tueda commented Apr 5, 2021

I think in general (or at least some cases) it is better to explicitly specify the error code that one wants to ignore by type: ignore[<code>], rather than just writing type: ignore. But the lack of the feature to ignore multiline code blocks, type: ignore[<code>] consumes much space in each line, some hacky tricks may be needed to cooperate with black or other formatters, and eventually the code could become less readable.

@zackees
Copy link

zackees commented Dec 15, 2022

This is something that I'd like to have and have thought about it a little. A potential approach is to use a special comment for this:

import numpy  # mypy: ignore

We could also support an if statement for this (this actually partially implemented already but completely undocumented):

from typing import MYPY

if not MYPY:
    import numpy

The value of MYPY would always be False at runtime, but when type checking it would be considered as a compile-time constant with value True that could be used for a kind of conditional compilation -- a if not MYPY block would be ignored by the type checker (but any newly defined names would be given the type Any, perhaps).

Or maybe we can support using a comment to ignore multiple lines:

# mypy: begin ignore
import numpy
import matplotlib.pyplot
import scipy.stats
# mypy: end ignore

Implementation of #486 would be awesome but it does not help when there are C extension modules. However, we may be able to automatically generate pure dynamically typed stubs for C extension modules automatically. @ashleyh started writing a stub generator (I have a clone at https://github.com/JukkaL/mypy-stubgen) but there hasn't been updates in a long time.

Yes, warnings need to be blocked out. I have very strict type checking but ironically mypy just hates pydantic. I can't ignore the line because the auto-formatter in VSCode keeps on putting the ignore comment on the wrong line.

I mean, why can't a feature like block disable or whole file ignore for a specific warning type be implemented? At this point my only resolution is to put a # mypy: ignore on the entire file. To make this safer I have to put the offending pydantic class into it's own file so I can still have type checking everywhere else.

This granularity just sucks. It's either per line or the entire file. pylint and flake8 allow disabling whole file warnings/errors, mypy should just do the same thing everyone else is doing.

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

10 participants