Replies: 1 comment 2 replies
-
|
Just a quick note: I think the following quote is a bit misleading: "Package maintainers who wish to support type checking of their code MUST add a marker file named py.typed to their package supporting typing." This only applies when distributing libraries. The py.typed file is then used as a marker for code using this library: "Hey, this library is typed, so use the type annotations from the library functions you are using." When checking your own code, py.typed is not necessary. This also means that for "application code", I would not distribute py.typed, only for libraries where the public API is sufficiently annotated. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm working on widespread typing adoption at my workplace, at least for libraries shared across teams. The typing ecosystem has matured a lot since last time people tried, especially with the adoption of PEP 561 and mypy being much better at working with dependencies. I'm finding that almost every one of our codebases should have a
py.typedfile, and this is leading me to wonder - when should codebases not have one?PEP 561 says, "Package maintainers who wish to support type checking of their code MUST add a marker file named py.typed to their package supporting typing." But it doesn't really define what "support type checking" means, and I think in practice the
py.typedfile is more useful for downstream users of the package who wish to type check their code than for the package's own maintainer.In particular, even if your package has no annotations at all, it seems like having a
py.typedfile is at least harmless and actually of positive value: mypy (for instance) will detect what variables and function signatures are in your package, type them asAny, and allow downstream users to make progress gradually typing their code. However, without apy.typedfile, mypy will raise a "module is installed, but missing library stubs or py.typed marker" error. You can suppress this, but this is a more annoying user experience for the downstream user, and if I understand correctly it will type the entire module asAny, reducing the amount of static validation that mypy can do.So I think that packages should have
py.typedfiles unless you specifically know you should not have one, even if you aren't interested in adding typing annotations or type-checking your own code, and I think the reasons you should not have apy.typedfile are:Optionalrule and you haven't had the time to fix it yet).Any.(However, since explicit
-stubspackages have a higher priority than types in the package itself, it seems like having apy.typedfile is harmless if you have some other way of communicating this.)__getattr__, etc.), and so trying to infer the contents of the package would return misleading information. In this case it's preferable to ship.pyifiles and add apy.typedmarker, but if you don't want to write types at all, you should leave out thepy.typedfile.Is this correct?
In other words, should templates for new projects include a
py.typedfile by default (as requested in e.g., audreyfeldroy/cookiecutter-pypackage#680) and should basically all Python packages (both public and in-house) that don't fall into one of the cases above go add apy.typedfile even if they don't have type annotations?On a related note, what are the rules about the contents of the
py.typedfile? If I'm going to addpy.typedfiles to thousands of internal libraries that don't have type annotations yet, I'd kind of like to include some explanation of why it's okay for that file to be there and why you probably shouldn't remove it. PEP 561 doesn't say much about it, except that partial stubs "MUST includepartial\nin apy.typedfile." I'm not sure if "include" means the full contents must be exactlypartial\nor whether it simply must be a substring. Outside of stubs (in particular, in actual packages, which is most of what I'm interested in), should I feel free to put something in thepy.typedfile? Should I use comment characters to avoid it being parsed in the future? (If I'm reading mypy's currrent implementation right, it doesn't care about contents for normal packages, and for stubs it checks whether the entire file.decode().strip() == "partial".)Beta Was this translation helpful? Give feedback.
All reactions