Hide gf2 sub-classes even more

- the `gf2` sub-classes `GF2One` and `GF2Zero` have no purpose
  other than to provide unique `help(one)` and `help(zero)` messages
- delete them at the bottom of `lalib.elements.galois`
  to prevent them from being imported at all
  (`type(one)` and `type(zero)` still appear to be the `gf2` class,
   which is a little white lie, provided by some meta class)
- differentiate between official and inofficial API in the test suite
This commit is contained in:
Alexander Hess 2024-09-19 15:36:10 +02:00
parent 348cd53767
commit 06d003b615
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
2 changed files with 29 additions and 5 deletions

View file

@ -523,6 +523,10 @@ class GF2Element(metaclass=GF2Meta):
numbers.Rational.register(GF2Element)
# The `GF2One` and `GF2Zero` sub-classes' primary purpose
# is to give `one` and `zero` their very own `help()` message
class GF2One(GF2Element):
"""The Galois field value `one`."""
@ -544,3 +548,5 @@ gf2 = GF2Element
del GF2Meta
del GF2One
del GF2Zero

View file

@ -14,15 +14,16 @@ import pytest
from lalib.elements import galois
one, zero = (
gf2, one, zero = ( # official API outside of `lalib.elements.galois`
galois.gf2,
galois.one,
galois.zero,
)
gf2, GF2One, GF2Zero = (
galois.gf2,
galois.GF2One,
galois.GF2Zero,
GF2Element, GF2One, GF2Zero = ( # not part of the official API
galois.GF2Element,
type(galois.one), # The `GF2One` and `GF2Zero` sub-classes
type(galois.zero), # are deleted in `lalib.elements.galois`
)
_THRESHOLD = galois.THRESHOLD
@ -87,11 +88,28 @@ def test_thresholds():
assert within_threshold < default_threshold < not_within_threshold
class TestGF2SubClasses:
"""Test the sub-classes behind `one` and `zero`."""
def test_gf2_is_an_alias(self):
"""The "`gf2` type" is really just `GF2Element`."""
assert gf2 is GF2Element
@pytest.mark.parametrize("cls", [GF2One, GF2Zero])
def test_sub_classes_for_gf2(self, cls):
"""`GF2One` and `GF2Zero` are sub-classes of `gf2`."""
assert issubclass(cls, gf2)
@pytest.mark.parametrize("cls", [gf2, GF2One, GF2Zero])
class TestGF2Casting:
"""Test the `gf2` class's constructor.
`gf2(value, ...)` returns either `one` or `zero`.
The sub-classes behind `one` and `zero` provide the
same functionality as `gf2` and have the sole purpose
of providing a unique `help()` message for `one` and `zero`.
"""
@pytest.mark.parametrize("value", strict_one_like_values)