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.
(Moved assignment and Milestone from the duplicate #964.)
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.
Yeah, I agree that this should be block-scoped.
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.
You can use the ignore_errors per-file option.
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
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
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 :)
@ddfisher Are you planning to work on this?
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.
Sorry we dropped the ball here -- we're a small team with many tasks. Do you want to try submitting a PR?
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.
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.
@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.
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.
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
@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.
Fantastic! I thought this was for the entire file but it's for the block only, right?
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.
Gotcha! Thank you :)
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.
@Beefy-Swain You can. See https://mypy.readthedocs.io/en/latest/config_file.html.
I鈥檓 currently working on a PR for this. I should have something up in the next couple of days.
@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
@danielbraun89 # type: ignore comment at the beginning of the file is now supported. Does it not work for you?
You can also use # mypy: ignore-errors at the top of the file.
Both doesnt work for me, I guess the problem in my side
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
I'm using 0.740 checking a Python 2 repo and neither
# type: ignorenor# mypy: ignore-errorsat 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.
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: """
Most helpful comment
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--excludeargument.In my case, I want to exclude automatically created Django migration files, but still scan my entire Django app.