4

I installed a "dnspython" package with "pip install dnspython" under Ubuntu 22.10 and made a following short script:

#!/usr/bin/env python3

import dns.zone
import dns.query

zone = dns.zone.Zone("example.net")
dns.query.inbound_xfr("10.0.0.1", zone)

for (name, ttl, rdata) in zone.iterate_rdatas("SOA"):
    serial_nr = rdata.serial

When I check this code snippet with mypy(version 0.990), then it reports an error: Module has no attribute "inbound_xfr" [attr-defined] for line number 7.

According to mypy documentation, if a Python file and a stub file are both present in the same directory on the search path, then only the stub file is used. In case of "dnspython", the stub file query.pyi is present in the dns package and the stub file indeed has no attribute "inbound_xfr". When I rename or remove the stub file, then the query.py Python file is used instead of the stub file and mypy no longer complains about missing attribute.

I guess this is a "dnspython" bug? Is there a way to tell to mypy that for query module, the stub file should be ignored?

Martin
  • 957
  • 7
  • 25
  • 38
  • 1
    I'd advice to install from git main branch, where they removed stub files and started using inline annotations. There is no `query.pyi` near [implementation](https://github.com/rthalley/dnspython/blob/master/dns/query.py) (though there still is one on branch 2.2) – STerliakov Nov 21 '22 at 21:21
  • [try exclude first](https://mypy.readthedocs.io/en/stable/error_codes.html#silencing-errors-based-on-error-codes) and/or add additional import statement `from dns.query import inbound_xfr` and use directly `inbound_xfr()` – storenth Nov 22 '22 at 17:14

3 Answers3

5

I would recommend ignoring only the specific wrong line, not the whole module.

dns.query.inbound_xfr("10.0.0.1", zone)  # type: ignore[attr-defined]

This will suppress attr-defined error message that is generated on that line. If you're going to take this approach, I'd also recommend running mypy with the --warn-unused-ignores flag, which will report any redundant and unused # type: ignore statements (for example, after updating the library).

Alexander Volkovsky
  • 2,588
  • 7
  • 13
3

Is there a way to tell to mypy that for query module, the stub file should be ignored?

No. Stub files have precedence over modules. Even if you pass the entire path of the stub file to --exclude, it will still see it.

You want to disable a language construct created specifically for definitions, which doesn't seem very logical.

I guess this is a "dnspython" bug?

Yes.

Anonymous Guy
  • 119
  • 2
  • 11
2

First of all, there is a option --exclude PATTERN to ignore files or directory to check.

According that doc, you should use --follow-imports option to skip the import module checked by mypy:

In particular, --exclude does not affect mypy’s import following.

You can use a per-module follow_imports config option to additionally avoid mypy from following imports and checking code you do not wish to be checked.

Another way, you could configure the Stub files in a specific directory, and using it by export MYPYPATH.

Victor Lee
  • 2,467
  • 3
  • 19
  • 37