Replies: 1 comment 9 replies
-
To clarify, this isn't specific to class attributes. It applies to all symbols that comprise the public interface of a library that includes a "py.typed" marker and therefore claims to be "typed". Can we agree that it's in the interests of the consumers of a typed library to have consistent behaviors among all type checkers and language servers? If we can agree on that point, then the next step is to discuss how best to achieve this objective. I authored the initial draft of the guidance that you cited above, and it represents my best attempt to achieve this objective. If you have other proposals, let's discuss them. As I mentioned in the guidance document and in other threads on this topic, a variable assignment is not the same as a type declaration, at least from the perspective of a type checker. A type declaration is a clear statement of intent that has well-defined meaning in the Python typing specifications (PEP 484, 526, etc.). When a variable has no type annotation, the intent (from a typing perspective) is left to interpretation. Type checkers will generally attempt to infer the type of the variable based on its assignment(s), but the mechanisms by which this are done are not standardized, and they vary by type checker. In a simple case like We could potentially carve out some specific rules for how certain trivial inference cases are handled. We could document those as part of the typing standard, and all of the type checkers could adopt those behaviors. That approach would require us to also clearly specify what cases are not standardized. Alternatively, we could collectively agree that consistent type interpretations for typed libraries across the Python ecosystem is not a goal, and we can leave the interpretation of types up to each tool. That would seem to run counter to the significant efforts over the past several years to improve static type analysis within the Python community. Static type analysis provides many benefits beyond finding bugs at edit time. It also powers time-saving features like completion suggestions, function signature help, and in-place docstring viewing. @dvarrazzo, it sounds like your major concern here is that it's a burden for library maintainers to include type annotations for the symbols that comprise the library's public interface. Is my understanding correct, or do you have other objections or concerns? If my understanding of your concern is correct, perhaps there are ways we can mitigate that burden through tooling, community contributions, etc. For example, I've invested in a "--verifytypes" mode in pyright that will help identify which symbols in a "py.typed" library are missing type annotations. Many library authors have started using this tool over the past year to make their libraries "type complete" and keep them so. For full transparency, I'm the primary author of pyright, Microsoft's open-source Python type checker. Pyright forms the core of Pylance, the default Python language server for VS Code which is used by millions of Python developers. My colleagues and I care deeply about making the Python development experience as enjoyable and productive as possible. I think it's safe to say that everyone in the Python typing community share these goals. Let's continue to work together to figure out the best way to do this. |
Beta Was this translation helpful? Give feedback.
-
It has been raised to my attention (psycopg/psycopg#212) that Python typing tools are going in the direction of applying different, stricter rules to class attributes compared to the inference rules used for normal variables. From https://github.com/python/typing/blob/master/docs/source/libraries.rst#type-completeness:
As far as I can see, humans can see, computers can see, the type of the
height
attribute is specified as a float. Should the class author wanted to allow broader types, they could specify its type:float | None
orfloat | complex
. But the type of the expression being transferred, by default, to the attribute, is not only a reasonable thing: doing otherwise is inconsistent with how types are inferred in local variables.As far as I can see (#913) there is no consensus for this change.
As a library author I don't want type annotation to become more of a chore than already is.
I would appreciate if this choice will be reconsidered.
Beta Was this translation helpful? Give feedback.
All reactions