-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Ignore typing errors in individual blocks #6948
Comments
See #6830 (the PR that closed #626). It’s not currently possible to get the line number of the colon that immediately precedes an indented block from the AST alone... at least not without resorting to error-prone guessing. We can't reliably tell the difference between this: def f() -> (
None
): # type: ignore
... And this: def f() -> (
None):
# type: ignore
... So I think block-scoped ignores using a I wanted this also, but I don't think it's worth changing mypy's source parsing 🙁. |
You can use the |
Why is this issue still open? |
The original issue asked for something slightly different (block-scoped type-ignore), which is a bit different from I'm not sure block-scoped type-ignores are all that necessary, but it's a reasonable request. |
Try with: from typing import Any, cast
import api as untype_api # It is import to use a different name to untyped module
api = cast(Any, untype_api)
class MyAbstraction:
def some_call(self) -> int:
result = api.untyped_call() # this shouldn't show type warnings.
return result |
It would be nice if @typing.no_type_check would be also a class decorator |
@jentyk According to the docs it should work as well as a class decorator, but it isn't apparently true. Mentioned here #607. |
pylint supports this type of pragma, so it is clearly theoretically possible. |
Also
to a function. You have to ignore everything or write a separate comment for each instance. And if you add a new decorator for this purpose, then using it would mean adding a runtime dependency to mypy just for that. |
What about introducing two comments to disable and enable specific rule(s) between them? For example: class MyAbstraction:
def __init__(self, url: str) -> None:
self.hostname = url.host
def large_call(self) -> int:
# type: off[no-untyped-call]
result_1 = api.untyped_call_1()
result_2 = api.untyped_call_2()
result_3 = api.untyped_call_2()
result_4 = api.untyped_call_2()
result = result_1 + result_2 + result_3 + result_4
return result
# type: on[no-untyped-call] This is also similar to formatters such as Black which uses |
What about having something like import typing
class MyAbstraction:
...
def another_large_call(self) -> int:
x : int = f() # This is checked
with typing.no_type_check: # no lines inside this context are checked
result_1 = api.untyped_call_1()
result_2 = api.untyped_call_2()
result = result_1 + result_2
y: str = g() # this is also checked
return result |
I don't like the context-manager idea. Type-hints are just hints with no behaviour during runtime. They are PEP-3107 "annotations". The I initially thought about a This is a "gut-feeling" and may be subjective as I have no concrete proof that this can cause problems. If there are any, I'd be happy to learn. |
Another case... I have code like this: class TurnLogAdmin(ModelView, model=TurnLog):
column_list = [
TurnLog.id,
TurnLog.turn_id,
TurnLog.timestamp,
TurnLog.key,
] # type: ignore Due to issues between SQLModel (which mashes SQL Alchemy and Pydantic models together, resulting in difficult typing situation) and sqladmin (whose typings expect SQL Alchemy model fields here)... mypy generates a type error for each element of the list:
I was hoping the FWIW #6648 and #1032 seem to intend that the ignore comment would cover the whole expression but perhaps multi-line list literal is a different construct? (I'm not sure exactly what does and doesn't count as an 'expression' ... https://docs.python.org/3/reference/expressions.html seems to include list literals though) |
For those who wants this for the |
A solution for blocks or class annotation would come in handy. Mypy can't handle things going on inside django ORM constraint definitions. |
It would be great if there's start and stop comments to pause mypy's type check, like
cuz i do need to exclude only a block of code (i.e., an if block) |
It is already possible to silence errors using
# type: ignore
on individual lines, and - since #626) also for a whole file.Additionally it would be nice to ignore errors on individual blocks. Especially on a class/function level when abstracting away an untyped library. For example:
When running
mypy
in "strict" mode the code will be littered with messages likeCall to an untyped function ... in a typed context
. Currently the only options are:# type: ignore
on each line causing errors by the external library# type: ignore
to the fileThe second option is best used if a module contains only calls to the external library. For new projects this is doable, but older/larger code-bases often need some refactoring for this. And this can be error-prone and seems risky to do only for type-hinting.
Having the option to disable errors on either a class-level or block-level would add a lot of flexibility.
The text was updated successfully, but these errors were encountered: