Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.

TryteString.random() use cls.LEN as its default length, when available #167

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion iota/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class TryteString(JsonSerializable):
IMPORTANT: A TryteString does not represent a numeric value!
"""
@classmethod
def random(cls, length):
def random(cls, length=None):
# type: (int) -> TryteString
"""
Generates a random sequence of trytes.
Expand All @@ -60,6 +60,12 @@ def random(cls, length):
alphabet = list(itervalues(AsciiTrytesCodec.alphabet))
generator = SystemRandom()

try:
length = length or cls.LEN
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would also trip if length == 0; could we change this to an explicit is None check?

except AttributeError: # class has no LEN attribute
if length is None:
raise TypeError("random() missing 1 required positional argument: 'length'")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this might be confusing to some users, since the method signature shows length as optional.

Maybe we could make this error explain that {class} does not define a length property.


# :py:meth:`SystemRandom.choices` wasn't added until Python 3.6;
# for compatibility, we will continue to use ``choice`` in a loop.
# https://docs.python.org/3/library/random.html#random.choices
Expand Down