-
-
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
Support a project/repo-wide config file #1249
Comments
One potential feature that this doesn't seem to support is automatically finding files to check by grepping for e.g. |
A potential use case would be to enforce --disallow-untyped-defs on a per-file (or perhaps per-module) basis. A co-worker says they'd gotten a file fully typed and didn't want any regressions on that in the future. |
There is another issue about per-file build flags: #1235 |
IIRC @ddfisher is working on this -- David, how's this going? |
+1 for this feature. |
I'm looking into this. As an intermediate step I propose to support a configuration file, default
I also propose to add
will read arguments (flags and filenames to type-check) from file Why two different formats? The This proposal is an intermediate step -- it doesn't support per-file flags yet. For that I propose to use dynamic sections with additional flags, where the section name is something like |
Also support reading command line flags using `mypy @flagsfile`. Addresses #1249 (but does not completely fix it). The mypy.ini file has the format: ``` [mypy] silent_imports = True python_version = 2.7 ``` Errors in this config file are non-fatal. Comments and blank lines are supported. The `@flagsfile` option reads additional argparse-style flags, including filenames, from `flagsfile`, one per line. This is typically used for passing in a list of files, but it can also be used for passing flags: ``` --silent-imports --py2 mypy ``` This format does not allow comments or blank lines. Each option must appear on a line by itself. Errors are fatal. The mypy.ini file serves as a set of defaults that can be overridden (or in some cases extended) by command line flags. An alternate config file may be specified using a command line flag: `--config-file anywhere.ini`. (There's a trick involved in making this work, read the source. :-)
More thoughts on per-file options There's a litmus test for whether an option could potentially vary per file: is it (or should it be!) included in OPTIONS_AFFECTING_CACHE? Note that this is currently missing strict-optional[-whitelist]; see #2126. There's a fairly easy choke point for per-file options; or at least, two choke points that should hopefully handle everything. These are the visit_file() methods in TypeChecker and SemanticAnalyzer. Here's a possible design:
That's it! The semantic analysis and type checking phases should now support per-file options. |
I've got all that implemented in @2148 too. Now I'm concerned about how to shoe-horn in the one flag that already has per-file behavior, Maybe it can be done as follows:
So then here's how you would specify the effect of
This will enable strict None checking globally, and suppress the errors in all files except those matching the glob pattern Alternatively, if you wanted to hide the errors in
Or maybe we should reverse the sense of the per-file flag and name it |
Also support reading command line flags using `mypy @flagsfile`. Addresses #1249 (but does not completely fix it). The mypy.ini file has the format: ``` [mypy] silent_imports = True python_version = 2.7 ``` Errors in this config file are non-fatal. Comments and blank lines are supported. There are also sections with glob patterns for per-file options, e.g. `[mypy-dir1/*,dir2/*]` (I'll document those later). The `@flagsfile` option reads additional argparse-style flags, including filenames, from `flagsfile`, one per line. This is typically used for passing in a list of files, but it can also be used for passing flags: ``` --silent-imports --py2 mypy ``` This format does not allow comments or blank lines. Each option must appear on a line by itself. Errors are fatal. The mypy.ini file serves as a set of defaults that can be overridden (or in some cases extended) by command line flags. An alternate config file may be specified using a command line flag: `--config-file anywhere.ini`. (There's a trick involved in making this work, read the source. :-)
Can we close this now and create new issues for any remaining work? |
Sure. What remaining work/ideas are there? |
We have a couple of different ad-hoc scripts for invoking mypy developing in different repositories where people are using it, which generally
--py2
and--silent-imports
, andHaving bits of shell script grow is never fun, and the logic to find what files to run on isn't totally trivial -- it ends up something like "files with
*.py
names that match# type:
with grep." Even as I write that it's actually more subtle as# type:
is both slightly too strict (#type:
, no space, is a perfectly good start to a type comment) and too loose (string literals.)Moreover, when there's a complicated script and the arguments to the underlying
mypy
become hard to identify, then when people are debugging an issue and need to vary themypy
invocation at the command line they can end up reconstructing versions that miss some of the details, which leads to a super frustrating debugging session -- @gvanrossum saw this happen yesterday.So I think it'd be really helpful to get to a point where a user can sit anywhere inside the source tree of a codebase that uses Mypy, and type just the command
to have the typechecker run in the normal way over that codebase. Plus arguments as desired to modify what it does, but starting from that normal configuration.
The basic tactic that a lot of development tools take to make that kind of interface possible (including Hack,
git
,arc
, and many others) is to haveSo e.g. a file
.mypy
, and when themypy
command is run, it looks up in the filesystem to find such a file. If there, then (a) the default operation is on all annotated files in the tree (by some specified definition), and (b) it's read as aconfigparser
config file, which could be empty or could specify the equivalent of--py2
,--silent-imports
, etc.Possible variations include
mypy
as the command to get this behavior, we start giving ourselves room for subcommands:mypy check
means "run the typechecker on this project, requiring a.mypy
file"mypy ls
could mean "list what files you would typecheck", which will be very helpful in some debugging situations especially in the presence of--silent-imports
mypy check.py
is awkward, but we could say something like "if it ends in.py
or/
it's a file, otherwise it can only be a subcommand" -- just existing invocations likemypy myscript
would breakmypy
/cc @ddfisher who was also discussing this today.
The text was updated successfully, but these errors were encountered: