petl-developers / petl

Python Extract Transform and Load Tables of Data
MIT License
1.22k stars 190 forks source link

DeprecationWarning / TypeError: Seeding based on hashing is deprecated/removed #657

Closed bmos closed 3 months ago

bmos commented 3 months ago

Problem description

What's happenning

When creating a dummytable, the warning is emitted:

DeprecationWarning: Seeding based on hashing is deprecated since Python 3.9 and will be removed in a subsequent version. The only supported seed types are: None, int, float, str, bytes, and bytearray.

Furthermore, when using the same code on 3.11 or 3.12, I get this error instead:

self = <random.Random object at 0x564b245ce8f0>
a = datetime.datetime(2024, 3, 11, 1, 27, 10, 858705), version = 2

    def seed(self, a=None, version=2):
        """Initialize internal state from a seed.

        The only supported seed types are None, int, float,
        str, bytes, and bytearray.

        None or no argument seeds from current time or from an operating
        system specific randomness source if available.

        If *a* is an int, all bits are used.

        For version 2 (the default), all of the bits are used if *a* is a str,
        bytes, or bytearray.  For version 1 (provided for reproducing random
        sequences from older versions of Python), the algorithm for str and
        bytes generates a narrower range of seeds.

        """

        if version == 1 and isinstance(a, (str, bytes)):
            a = a.decode('latin-1') if isinstance(a, bytes) else a
            x = ord(a[0]) << 7 if a else 0
            for c in map(ord, a):
                x = ((1000003 * x) ^ c) & 0xFFFFFFFFFFFFFFFF
            x ^= len(a)
            a = -2 if x == -1 else x

        elif version == 2 and isinstance(a, (str, bytes, bytearray)):
            if isinstance(a, str):
                a = a.encode()
            a = int.from_bytes(a + _sha512(a).digest())

        elif not isinstance(a, (type(None), int, float, str, bytes, bytearray)):
>           raise TypeError('The only supported seed types are: None,\n'
                            'int, float, str, bytes, and bytearray.')
E           TypeError: The only supported seed types are: None,
E           int, float, str, bytes, and bytearray.

/opt/hostedtoolcache/Python/3.11.8/x64/lib/python3.11/random.py:160: TypeError

Expected behavior

The dummytable should be created with no warning or error.

Scenario for reprodution

Reproducible test case

Please provide a minimal, reproducible code sample, a copy-pastable example if possible:

I haven't been able to make one yet, but changing this in the installed copy of petl in my .venv folder resolves the warning.

Version and installation information

Please provide the following:

Also, if you think it might be relevant, please provide the output from pip freeze or conda env export depending on which was used to install petl.

Additional context

Add any other context about the problem here.

Also, feel free to remove all sections and text that aren't relevant.