-
-
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
Controlling scope of build options #1235
Comments
I like the per-file flags in the source code. Maybe we could also have
command-line flags to override these or perhaps to change the default? It
might make sense to try a code base with a more strict flag to see how it
responds. A less strict mode doesn't make as much sense.
…--Guido (mobile)
|
I agree: per-file flags in the source code seems like the clear winner here. The downside of implementing them sooner rather than later is that it locks us into the names we choose much more than command-line flags do, in my opinion, and I don't feel like we have our naming scheme sorted out yet. Have we been getting feature requests for this yet? That said, I actually don't find problems A or C particularly convincing. I think continuing to have command-line flags is important. Here are some benefits that I see:
I think Guido brought up an interesting question: should command-line flags override per-file flags, or just set the default? Each supports a different use case: Both of these seem common enough to me that I think we should consider eventually supporting both. By default, command-line flags would just set the default option, but there should be some Finally, I disagree about |
Maybe we can close this now we have a mypy.ini file with per-file options? |
I think the mypy.ini file (and its globbing) is useful, but I think it's a complement to per-file options, in part because it's too far away from the source in question (and would need to be updated if a file moved, for example, which would be easy to forget). |
Yeah, there are definitely use cases that are better dealt with through the config file and other use cases that are better off using in-file options. So it's good we are pursuing both. |
Mypy now supports in-file options, e.g. |
Currently the
--implicit-any
mode flag is global, as it uniformly affects how mypy processes every file in a mypy run. Other global flags are being considered or implemented, including--strict
and the "weak mode". Similarly, we could have options to disable/enable individual errors/warnings. These kinds of mode flags have some inconvenient properties, and I elaborate on this below.A. Combining type checked programs is hard
It's hard to combine two independently developed codebases into a single type checked program, as it may be necessary to use the least common denominator options to type check the combined program without spurious warnings. Global flags effectively introduce multiple mypy dialects that aren't quite compatible. Using the least common denominator options is clearly not desirable as programmers likely chose to enable some options for their code for a good reason.
Integrating multiple pieces of code without loosening type checking may involve a lot of refactoring or adding
# type: ignore
comments.B. Migrating to stricter type checking mode is hard
Migrating from the default mode to
--strict
, for example, would have to be performed for the whole code base to avoid generating warnings. If there is, say, millions of lines code, this can be a huge undertaking, and may require the co-operation of owners of every part of the codebase. It would be much nicer to migrate to stricter options gradually, similar to how mypy supports migration from dynamic to static typing in small increments. This would also give more flexibility to owners of particular modules.C. Options aren't self-documenting
When just looking at a source file, it's not obvious under which options it's supposed to type checked. This is less of a problem within a single, uniform codebase, but it becomes more important when combining code from multiple sources. It's easy to "forget" that some files should be checked in the strict mode when copying or moving source code, for example, since the knowledge lives in external configuration such as build scripts instead of being close to the physical code.
Here are some ways to overcome some of these problems:
--strict=package1,package2
that will enforce some options for particular packages only. Alternatively, we could have a configuration file with rules such asstrict=package/**,module
, or perhaps even a hierarchy of such configuration files. This provides more flexibility but it makes it easy to forget to modify the configuration when adding new files or moving files around.# mypy: strict
near the top of the file that change mode options for that file only. This is very flexible and it makes files self-contained, but it's easy to forget to add the desired options to new files. We could also have a tool that enforces that certain options are always used in certain packages, similar to (2) above, in addition to per-file comments.My proposal is to choose some variant of option 3.
Note that I don't consider
--silent-import
a global option because it only affects files not being processed, so it's not really problematic in the same sense as the other options I discussed above.The text was updated successfully, but these errors were encountered: