13

I wish to use the options disable_error_code = ["name-defined"] and ignore_missing_imports = true only for some specific modules, but I am struggling to make it work. The following is an excerpt of my non-working pyproject.toml file:

[tool.mypy]
python_version = "3.9"
disallow_untyped_defs = true
show_error_codes = true
no_implicit_optional = true
warn_return_any = true
warn_unused_ignores = true
exclude = ["scripts", "docs", "test"]


[[tool.mypy.overrides]]
module = [
    "firstmodule",
    "secondmodule",
    "utils",
    "config",
]
disable_error_code = ["name-defined"]
ignore_missing_imports = true

More specifically, if I keep disable_error_code = ["name-defined"] as indicated above, then I get the following kind of errors:

pyproject.toml: [module = "utils"]: Per-module sections should only specify per-module flags (disable_error_code)

If I keep ignore_missing_imports = true as indicated above, then it is ignored and errors due to missing import are signaled.

If instead I move the mentioned two options under [tool.mypy] everything works.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Barzi2001
  • 989
  • 8
  • 24

3 Answers3

8

I also wanted to selectively disable warnings for packages that don't yet have type hints, and this approach seems to work for me:

[[tool.mypy.overrides]]
module = "firstmodule.*"
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "secondmodule.*"
ignore_missing_imports = true
bad_coder
  • 11,289
  • 20
  • 44
  • 72
Jeff Garbers
  • 181
  • 2
  • Thanks Jeff, perhaps you can accept this answer @Barzi2001 ? This worked for me. I was getting `module is installed, but missing library stubs` for a particular range of modules. Using `[tool.mypy-azureml.*]` was being rejected from the `pyproject.toml`. Mypy documentation mentions `pyproject.toml` as a valid config source but studiously ignores what syntax can be used to support module-specific sections. Perhaps they want to discourage use of `pyproject.toml`. I added an overrides section as Jeff describes with `module = "azureml.*"` in that section and `ignore_missing_imports` was respected. – NeilG Jan 24 '23 at 03:02
4

I found that I could use a single [[tool.mypy.overrides]] section with a comma-separated list of package names, when disabling missing import warnings - e.g.

[[tool.mypy.overrides]]
module = "firstmodule.*,secondmodule.*"
ignore_missing_imports = true
Will Keeling
  • 22,055
  • 4
  • 51
  • 61
1

Up to now, there are too many options that are only supported globally. Amongst them, disable_error_code. In particular, it will warn you that

Per module sections should only specify per-module flags

Sources:

gabopushups
  • 185
  • 2
  • 11
  • can you provide a source? – Bartek Lachowicz Apr 19 '23 at 15:49
  • @BartekLachowicz [Per module and global options](https://mypy.readthedocs.io/en/stable/config_file.html?highlight=Per%20module%20sections%20should%20only%20specify%20per-module%20flags#per-module-and-global-options) - [disable_error_code](https://mypy.readthedocs.io/en/stable/config_file.html?highlight=disable_error_code#confval-disable_error_code) – gabopushups May 28 '23 at 00:52
  • Thanks! @gabopushups. OOC how many push ups can you do at once? – Bartek Lachowicz May 28 '23 at 13:14