Python attributes, __slots__, and API design
Python attributes, __slots__, and API design
Posted Jul 7, 2021 20:26 UTC (Wed) by Sesse (subscriber, #53779)In reply to: Python attributes, __slots__, and API design by mb
Parent article: Python attributes, __slots__, and API design
Posted Jul 7, 2021 20:42 UTC (Wed)
by mb (subscriber, #50428)
[Link] (3 responses)
That's not how typing works in Python.
Posted Jul 7, 2021 21:52 UTC (Wed)
by NYKevin (subscriber, #129325)
[Link] (2 responses)
* Optional syntax for annotating the types of things. The interpreter treats this syntax as a glorified comment. It checks for basic syntactic validity and NameError, and that's about it. If you say that the type of an object is "12", or don't indicate a type at all, the interpreter is perfectly happy with that. If you write a syntax error, it will yell at you, but that's hardly a problem since you can just omit annotations altogether.
Hypothetically, if the entire Python community decided tomorrow that all new code must have static types, then the only problem you would have is that some people would file bugs against your project saying the linter doesn't like it. You can WONTFIX those bugs and carry on as usual, if that is your inclination. There should be no compatibility issues with anyone else's code. If you write library code, and your library is unannotated, then some people might be unhappy about that (because it would make the linter less accurate on their application code where it calls into your library), but that's arguably their problem, not yours.
(If you want, you can provide a set of type hints for your external API without having to type hint every line of code inside your library. This is particularly useful for C extensions, which otherwise would not be possible to annotate.)
Posted Jul 7, 2021 23:03 UTC (Wed)
by Sesse (subscriber, #53779)
[Link] (1 responses)
Posted Jul 7, 2021 23:30 UTC (Wed)
by NYKevin (subscriber, #129325)
[Link]
Python attributes, __slots__, and API design
Python attributes, __slots__, and API design
* The typing module, which includes a number of classes that let you write things like Union[foo, bar] or Optional[T]. The resulting objects are (for the most part) just opaque blobs that have reasonable-looking repr() text. They generally don't have any actual logic in them, and are just designed to remember the arguments you passed in.
* A set of linting rules for checking the types of things. CPython itself does not include a reference implementation of those rules, so the CPython interpreter is incapable of applying them. Instead, you have to download a third-party linter such as mypy. Obviously, the linter will be unhappy if an object's type is "12", but if you're running the linter, then you presumably wanted to be warned about that... right?
Python attributes, __slots__, and API design
Python attributes, __slots__, and API design
