Skip to content

Commit

Permalink
Allow Table(None)
Browse files Browse the repository at this point in the history
  • Loading branch information
austinweisgrau committed Feb 5, 2024
1 parent 237693f commit 62ceca6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 25 deletions.
24 changes: 2 additions & 22 deletions parsons/etl/table.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import pickle
from enum import Enum
from typing import Union

import petl
Expand All @@ -14,20 +13,6 @@
DIRECT_INDEX_WARNING_COUNT = 10


class _EmptyDefault(Enum):
"""Default argument for Table()
This is used because Table(None) should not be allowed, but we
need a default argument that isn't the mutable []
See https://stackoverflow.com/a/76606310 for discussion."""

token = 0


_EMPTYDEFAULT = _EmptyDefault.token


class Table(ETL, ToFrom):
"""
Create a Parsons Table. Accepts one of the following:
Expand All @@ -46,16 +31,11 @@ class Table(ETL, ToFrom):

def __init__(
self,
lst: Union[list, tuple, petl.util.base.Table, _EmptyDefault] = _EMPTYDEFAULT,
lst: Union[list, tuple, petl.util.base.Table, None] = None,
):
self.table = None

# Normally we would use None as the default argument here
# Instead of using None, we use a sentinal
# This allows us to maintain the existing behavior
# This is allowed: Table()
# This should fail: Table(None)
if lst is _EMPTYDEFAULT:
if lst is None:
self.table = petl.fromdicts([])

elif isinstance(lst, list) or isinstance(lst, tuple):
Expand Down
5 changes: 2 additions & 3 deletions test/test_etl.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ def test_from_invalid_list(self):
self.assertRaises(ValueError, Table, list_of_invalid)

def test_from_empty_petl(self):
# This test ensures that this would fail: Table(None)
# Even while allowing Table() to work
self.assertRaises(ValueError, Table, None)
# Just ensure this doesn't throw an error
Table(None)

def test_from_empty_list(self):
# Just ensure this doesn't throw an error
Expand Down

0 comments on commit 62ceca6

Please sign in to comment.