Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Table(None) #984

Open
wants to merge 1 commit into
base: major-release-4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
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