Skip to content

Commit

Permalink
Remove overlapping slots from AbstractKey subclasses
Browse files Browse the repository at this point in the history
`PublicKey` and `PrivateKey` both define the `n` and `e` slots, which
are already present in their base class. This reduces the benefits of
having slots.

```shell
$ slotscheck -m rsa -v
ERROR: 'rsa.key:PrivateKey' defines overlapping slots.
       - e (rsa.key:AbstractKey)
       - n (rsa.key:AbstractKey)
ERROR: 'rsa.key:PublicKey' defines overlapping slots.
       - e (rsa.key:AbstractKey)
       - n (rsa.key:AbstractKey)
```

The Python docs say:

> If a class defines a slot also defined in a base class, the instance
> variable defined by the base class slot is inaccessible (except by
> retrieving its descriptor directly from the base class). This renders
> the meaning of the program undefined.
  • Loading branch information
ariebovenberg authored and sybrenstuvel committed Mar 13, 2022
1 parent 6391b1a commit 3b31182
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions rsa/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class PublicKey(AbstractKey):
"""

__slots__ = ("n", "e")
__slots__ = ()

def __getitem__(self, key: str) -> int:
return getattr(self, key)
Expand Down Expand Up @@ -404,7 +404,7 @@ class PrivateKey(AbstractKey):
"""

__slots__ = ("n", "e", "d", "p", "q", "exp1", "exp2", "coef")
__slots__ = ("d", "p", "q", "exp1", "exp2", "coef")

def __init__(self, n: int, e: int, d: int, p: int, q: int) -> None:
AbstractKey.__init__(self, n, e)
Expand Down

0 comments on commit 3b31182

Please sign in to comment.