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

Allow using # type: ignore to skip type checking in a file #626

Closed
JukkaL opened this issue Mar 29, 2015 · 32 comments · Fixed by #6830
Closed

Allow using # type: ignore to skip type checking in a file #626

JukkaL opened this issue Mar 29, 2015 · 32 comments · Fixed by #6830
Labels

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Mar 29, 2015

As per PEP 484 discussion, it should be possible to use # type: ignore to skip type checking in the rest of a file:

# type: ignore
... # ignore type errors here

Currently the comment can only be used to ignore errors on individual lines.

@gvanrossum
Copy link
Member

(Moved assignment and Milestone from the duplicate #964.)

@JukkaL
Copy link
Collaborator Author

JukkaL commented Aug 17, 2016

I wonder what we should do if this happens within a function. Should we then ignore all errors within the function only? Example:

def f() -> None:
    # type: ignore
    return 1 + 'x'   # don't report an error?

a = 1 + 'y'   # report an error here?

This could be nice, as it would let people annotate functions but ignore errors in the body of the function. @no_type_check is not a good match, as it also causes the annotations to be ignored.

@gvanrossum
Copy link
Member

Yeah, I agree that this should be block-scoped.

@alexpirine
Copy link

alexpirine commented Feb 14, 2017

Hello,

Alternatively, could there be a way to exclude certain files or directories from command line or in the configuration file?

Currently, if I type mypy my_directory, the entire folder is scanned. It would be very handy to be able to add an --exclude argument.

In my case, I want to exclude automatically created Django migration files, but still scan my entire Django app.

@gvanrossum
Copy link
Member

gvanrossum commented Feb 14, 2017 via email

@alexpirine
Copy link

alexpirine commented Feb 14, 2017

Thank you @gvanrossum, this seems to be what I was looking for.

Could you please assist me a bit further? I tried to follow the instructions, but something is still not working:

mypy.ini

[mypy]

[gw_box.migrations.*]
ignore_errors = True
$ mypy gw_box --ignore-missing-imports --config-file mypy.ini
gw_box/migrations/0001_initial.py:14: error: Need type annotation for variable

@gvanrossum
Copy link
Member

You need to add a section like this:

[mypy-dropbox.edgestore.datatypes.namespace]
ignore_errors = False

For docs see http://mypy.readthedocs.io/en/latest/config_file.html

@alexpirine
Copy link

alexpirine commented Feb 14, 2017

Thank you, I figured out I was just missing the mypy- part.

P.S. Just figured out who you were. Huge thank you for creating Python :)

@gvanrossum gvanrossum removed this from the 0.4.x milestone Mar 29, 2017
@JukkaL
Copy link
Collaborator Author

JukkaL commented May 31, 2017

@ddfisher Are you planning to work on this?

@chingc
Copy link

chingc commented Mar 17, 2018

I would love to see this feature get implemented. In my tests I'm deliberately trying to break things, so being able to ignore the whole file is very useful. I'm currently using mypy.ini, but I'd much rather prefer adding a comment at the top of the file.

@gvanrossum
Copy link
Member

Sorry we dropped the ball here -- we're a small team with many tasks. Do you want to try submitting a PR?

@kamahen
Copy link
Contributor

kamahen commented Mar 17, 2018

Should there also be a #type: enable? This would be similar to pylint's disable/enable to mark arbitrary blocks of code as not type-checked.

@gvanrossum
Copy link
Member

That's not in the PEP, and presumably that should be done using one of the decorators already defined by the PEP for that purpose.

@kamahen
Copy link
Contributor

kamahen commented Mar 17, 2018

@no_type_check at the class/function level probably suffices -- so #type: ignore by itself would only be used at the file level. Perhaps make it explicit by #type: ignore file.

@ethanhs
Copy link
Collaborator

ethanhs commented Mar 17, 2018

I think we are pretty set on the syntax, as we don't want to make the parser implementation any more complicated for a special case. # type: ignore at the beginning of a block seems pretty straight forward to not type check that block.

@JukkaL JukkaL added the false-positive mypy gave an error on correct code label May 17, 2018
@Lawouach
Copy link

Hello folks,

I've started using mypy (quite happily) but there are a few checks that I would like to ignore while not ignoring the entire file. It's not clear to me whether this is actually feasible as of now.

For instance, I am using SQLlchemy and create model classes such as:

class Workspace(db.Model):

With this, mypy complains that db.Model is an invalid base class. Whether this is a valid message or not is one thing, but being able to ignore this message would be handy because it makes my experience more complicated since the file containing such definitions are always considered with problems.

I'd appreciate some feedback (might be doing something wrong here).

Cheers,
-Sylvain

@ilevkivskyi
Copy link
Member

@Lawouach SQLAlchemy is special, since it uses a lot of dynamic magic. We are working on better support for it. For now you can just place # type: ignore on the class definition line(s) where mypy report errors.

@Lawouach
Copy link

Fantastic! I thought this was for the entire file but it's for the block only, right?

@ilevkivskyi
Copy link
Member

A # type: ignore comment on a line currently suppresses only errors reported on this particular line. This issue is about adding a second option of ignoring a whole piece of file.

@Lawouach
Copy link

Gotcha! Thank you :)

@benjamin-kirkbride
Copy link

So is it still not possible to do this? I'm looking to disable mypy checking for tests and cannot find a good way to do this.

@mitar
Copy link

mitar commented May 12, 2019

@Beefy-Swain You can. See https://mypy.readthedocs.io/en/latest/config_file.html.

@brandtbucher
Copy link
Member

I’m currently working on a PR for this. I should have something up in the next couple of days.

gvanrossum pushed a commit that referenced this issue May 16, 2019
Fixes #626

(There are potential further features as discussed in the PR, but these are somewhat controversial and may even be excised from PEP 484.)
PattenR pushed a commit to PattenR/mypy that referenced this issue Jun 23, 2019
Fixes python#626

(There are potential further features as discussed in the PR, but these are somewhat controversial and may even be excised from PEP 484.)
@danielbraun89
Copy link

danielbraun89 commented Jul 22, 2019

@Beefy-Swain You can. See https://mypy.readthedocs.io/en/latest/config_file.html.

The proposed solution don't work in case the mypy.ini is being controlled by a different entity (DevOps for example).
There is certainly a need for a local solution

@JukkaL
Copy link
Collaborator Author

JukkaL commented Jul 22, 2019

@danielbraun89 # type: ignore comment at the beginning of the file is now supported. Does it not work for you?

@gvanrossum
Copy link
Member

You can also use # mypy: ignore-errors at the top of the file.

@danielbraun89
Copy link

Both doesnt work for me, I guess the problem in my side

@dargueta
Copy link

I'm using 0.740 checking a Python 2 repo and neither # type: ignore nor # mypy: ignore-errors at the top of a file work for me. My configuration looks like this:

[mypy]
mypy_path = ...
python_version = 2.7
disallow_any_unimported = false
disallow_any_decorated = true
disallow_untyped_calls = true
warn_redundant_casts = true
warn_unused_ignores = true
# HACK: Remove this as soon as possible
ignore_missing_imports = true

@kkpattern
Copy link

I'm using 0.740 checking a Python 2 repo and neither # type: ignore nor # mypy: ignore-errors at the top of a file work for me. My configuration looks like this:

[mypy]
mypy_path = ...
python_version = 2.7
disallow_any_unimported = false
disallow_any_decorated = true
disallow_untyped_calls = true
warn_redundant_casts = true
warn_unused_ignores = true
# HACK: Remove this as soon as possible
ignore_missing_imports = true

It seems like a bug they fixed in 0.750(see #7789). We also found per file ignore not working in 0.740 and confirmed 0.750 have fixed this.

@nedrebo
Copy link

nedrebo commented Apr 3, 2020

How can I apply this to line 3 in the following code? Comments are not allowed here. I tried to add it after """ on line 7, but that didn't work.

1: return f"""
2:    {'constexpr' if self.is_constexpr() else ''} ValueType verifyValue({argument}) {{
3:        return ({self.value_in_range_expression()})
4:            ? {return_value}
5:            : throw std::out_of_range("{self.out_of_range_exception_message()}");
6:    }}
7:    """

@ikamensh
Copy link

ikamensh commented Jun 9, 2021

The proposal for # type: ignore in the body of the function appears not to work yet:

the below code errors with mypy 0.901

def my_sum(*args: int) -> int:
    return sum(args)


def other() -> None:
    # type: ignore
    print(my_sum("1", 2))

@gvanrossum
Copy link
Member

We gave up on that, it didn't seem all that important.

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.