[issue40346] Add random.BaseRandom to ease implementation of subclasses

Matt Bogosian report at bugs.python.org
Thu Sep 23 10:09:31 EDT 2021


Matt Bogosian <eb3f73+python+org at yaymail.com> added the comment:

I landed here after investigating this surprising result:

  # test_case.py
  from random import Random
  from typing import Sequence, Union
  
  _RandSeed = Union[None, int, Sequence[int]]
  
  class MyRandom(Random):
    def __init__(
      self,
      seed: _RandSeed = None,
    ):
      if seed is not None and not isinstance(seed, int):
        seed = sum(seed)
      super().__init__(seed)
 
  MyRandom([1, 2])

Output:

  python ./test_case.py
  Traceback (most recent call last):
    File "/…/./test_case.py", line 16, in 
  <module>
      MyRandom([1, 2])
  TypeError: unhashable type: 'list'

In my observation, the Random class aspires to be an interface (and default implementation), but doesn't really live up to those aspirations. (See also https://github.com/python/typeshed/issues/6063.) I suspect nudging Random closer to its claims was the point of this proposal. I'm kind of sad it (or something like it) was rejected in favor of a process that will probably take years. Is there a reason not to do both, meaning heal what lives in the standard library now to live up to its own marketing *and* work toward a better interface in the future?

----------
nosy: +posita

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40346>
_______________________________________


More information about the Python-bugs-list mailing list