From 3bd3aec5eddaa56d38f1dda500341c556d2fe2d0 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Fri, 24 Sep 2021 20:29:50 +0300 Subject: [PATCH 1/6] Adds docs about new `__slots__` feature, refs #10801 --- docs/source/class_basics.rst | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/docs/source/class_basics.rst b/docs/source/class_basics.rst index 8e604427e683a..40e477dbf12cf 100644 --- a/docs/source/class_basics.rst +++ b/docs/source/class_basics.rst @@ -315,3 +315,33 @@ class, including an abstract method defined in an abstract base class. You can implement an abstract property using either a normal property or an instance variable. + +Slots +***** + +When a class has explicitly defined +`__slots__ `_ +mypy will check that all assigned attributes are listed there, +otherwise an error will be raised. + +.. code-block:: python + + class Album: + __slots__ = ('name', 'year') + + def __init__(self, name: str, year: int) -> None: + self.name = name + self.year = year + self.released = True # E: Trying to assign name "released" that is not in "__slots__" of type "Album" + + my_album = Album('Songs about Python', 2021) + +We have several rules when we count classes as "slotted" +(rules are the same as in CPython): + +1. All base classes (except builtin ones) must have explicit ``__slots__`` defined +2. ``__slots__`` do not have explicit ``'__dict__'`` value defined + +And we have some custom rules as well: + +1. All ``__slots__`` must be statically known. For example, no variables, only string literals From 34da7d90149e62b09f0b0bec628b7d1b8b1a8542 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Wed, 29 Sep 2021 20:09:34 +0300 Subject: [PATCH 2/6] Update class_basics.rst --- docs/source/class_basics.rst | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/source/class_basics.rst b/docs/source/class_basics.rst index 40e477dbf12cf..3e5209b6e96c4 100644 --- a/docs/source/class_basics.rst +++ b/docs/source/class_basics.rst @@ -340,8 +340,9 @@ We have several rules when we count classes as "slotted" (rules are the same as in CPython): 1. All base classes (except builtin ones) must have explicit ``__slots__`` defined -2. ``__slots__`` do not have explicit ``'__dict__'`` value defined +2. ``__slots__`` definition does not have include ``__slots__ = ('__dict__', )``. + Because it allows to set any possible attribute, almost as when no ``__slots__`` is defined -And we have some custom rules as well: - -1. All ``__slots__`` must be statically known. For example, no variables, only string literals +And we have a custom rule: +all ``__slots__`` must be statically known. +For example, no variables: only string literals. From 1f7c1c510e0766972c12c4a4b19c8811b7a663ec Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Fri, 1 Oct 2021 20:10:16 +0300 Subject: [PATCH 3/6] Addresses review --- docs/source/class_basics.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/source/class_basics.rst b/docs/source/class_basics.rst index 3e5209b6e96c4..2f206fbe8c26e 100644 --- a/docs/source/class_basics.rst +++ b/docs/source/class_basics.rst @@ -337,12 +337,12 @@ otherwise an error will be raised. my_album = Album('Songs about Python', 2021) We have several rules when we count classes as "slotted" -(rules are the same as in CPython): +(the rules are the same as in CPython): 1. All base classes (except builtin ones) must have explicit ``__slots__`` defined -2. ``__slots__`` definition does not have include ``__slots__ = ('__dict__', )``. +2. ``__slots__`` must not include ``__dict__``. For example, ``__slots__ = ("__dict__", ...)`` is not valid. Because it allows to set any possible attribute, almost as when no ``__slots__`` is defined And we have a custom rule: -all ``__slots__`` must be statically known. +all values in ``__slots__`` must be statically known. For example, no variables: only string literals. From 8b76c393020b18f7e4530dd1c08461721a532f2a Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Sun, 3 Oct 2021 14:59:37 -0700 Subject: [PATCH 4/6] Update docs/source/class_basics.rst --- docs/source/class_basics.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/source/class_basics.rst b/docs/source/class_basics.rst index 2f206fbe8c26e..4a59625ca38f1 100644 --- a/docs/source/class_basics.rst +++ b/docs/source/class_basics.rst @@ -321,8 +321,7 @@ Slots When a class has explicitly defined `__slots__ `_ -mypy will check that all assigned attributes are listed there, -otherwise an error will be raised. +mypy will check that all attributes assigned to are members of `__slots__`. .. code-block:: python From 06ea379463bd42464721f89cd3fa68c720d93730 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Mon, 4 Oct 2021 10:43:11 +0300 Subject: [PATCH 5/6] Update docs/source/class_basics.rst Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com> --- docs/source/class_basics.rst | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/docs/source/class_basics.rst b/docs/source/class_basics.rst index 4a59625ca38f1..82d346235f374 100644 --- a/docs/source/class_basics.rst +++ b/docs/source/class_basics.rst @@ -335,13 +335,9 @@ mypy will check that all attributes assigned to are members of `__slots__`. my_album = Album('Songs about Python', 2021) -We have several rules when we count classes as "slotted" -(the rules are the same as in CPython): +Mypy will only check attribute assignments against `__slots__` when the following conditions hold: -1. All base classes (except builtin ones) must have explicit ``__slots__`` defined -2. ``__slots__`` must not include ``__dict__``. For example, ``__slots__ = ("__dict__", ...)`` is not valid. - Because it allows to set any possible attribute, almost as when no ``__slots__`` is defined - -And we have a custom rule: -all values in ``__slots__`` must be statically known. -For example, no variables: only string literals. +1. All base classes (except builtin ones) must have explicit ``__slots__`` defined (mirrors CPython's behaviour) +2. ``__slots__`` does not include ``__dict__``, since if ``__slots__`` includes ``__dict__`` +it allows setting any attribute, similar to when ``__slots__`` is not defined (mirrors CPython's behaviour) +3. All values in ``__slots__`` must be statically known. For example, no variables: only string literals. From 9f295fa408ee7506ae0391a7b0510e0dbdefdd12 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Mon, 4 Oct 2021 10:51:07 +0300 Subject: [PATCH 6/6] Update class_basics.rst --- docs/source/class_basics.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/class_basics.rst b/docs/source/class_basics.rst index 82d346235f374..70d2dc6316310 100644 --- a/docs/source/class_basics.rst +++ b/docs/source/class_basics.rst @@ -339,5 +339,5 @@ Mypy will only check attribute assignments against `__slots__` when the followin 1. All base classes (except builtin ones) must have explicit ``__slots__`` defined (mirrors CPython's behaviour) 2. ``__slots__`` does not include ``__dict__``, since if ``__slots__`` includes ``__dict__`` -it allows setting any attribute, similar to when ``__slots__`` is not defined (mirrors CPython's behaviour) + it allows setting any attribute, similar to when ``__slots__`` is not defined (mirrors CPython's behaviour) 3. All values in ``__slots__`` must be statically known. For example, no variables: only string literals.