[issue41644] builtin type kwargs
New submission from Joseph Perez <joperez@hotmail.fr>: Class definition can have kwargs which are used by `__init_subclass__` (and `__prepare__`). However, passing these kwargs using `type` builtin function instead of class definition syntax is not documented; kwargs are not mentioned in the function signature. https://docs.python.org/3/library/functions.html#type However, passing kwargs to `type` works: ```python class Foo: def __init_subclass__(cls, **kwargs): print(kwargs) Bar = type("Bar", (Foo,), {}, bar=None) # mypy and Pycharm complain #> {'bar': None} ``` By the way, the possibility to pass kwargs in `type` call is not documented in https://docs.python.org/3/reference/datamodel.html#customizing-class-creatio... too. ---------- assignee: docs@python components: Documentation messages: 375936 nosy: docs@python, joperez priority: normal severity: normal status: open title: builtin type kwargs versions: Python 3.10, Python 3.6, Python 3.7, Python 3.8, Python 3.9 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue41644> _______________________________________
Serhiy Storchaka <storchaka+cpython@gmail.com> added the comment: It is rather an issue of MyPy and PyCharm. The Python interpreter itself does not perform static type testing and does not provide annotations for builtins. ---------- nosy: +serhiy.storchaka _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue41644> _______________________________________
Joseph Perez <joperez@hotmail.fr> added the comment: That's why it's not an interpreter issue but a lack in the official documentation where the signature is documented. I quote https://docs.python.org/3/library/functions.html#type:
class type(object) class type(name, bases, dict)
The second line should be "class type(name, bases, dict, **kwargs)". (I've mentioned Pycharm and Mypy, but i think it's a kind of side-effect of the incomplete official documentation on which is based their typeshed) In fact, I've raised this issue because I've found myself needing to instantiate a class using `type` and kwargs, and I've not found in the documentation an example of it. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue41644> _______________________________________
Change by Ethan Furman <ethan@stoneleaf.us>: ---------- nosy: +ethan.furman _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue41644> _______________________________________
participants (3)
-
Ethan Furman
-
Joseph Perez
-
Serhiy Storchaka