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

Commit a1552e6

Browse files
chebee7iashwoods
authored andcommitted
Add is_namedtuple().
1 parent 8530ba1 commit a1552e6

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

raven/utils/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
import sys
1616

1717
from raven.utils.compat import iteritems, string_types
18-
from raven.utils.basic import merge_dicts, varmap, memoize, once
18+
from raven.utils.basic import (
19+
merge_dicts, varmap, memoize, once, is_namedtuple
20+
)
1921

2022

2123
logger = logging.getLogger('raven.errors')

raven/utils/basic.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,14 @@ def new_func(*args, **kwargs):
8383
new_func = update_wrapper(new_func, func)
8484
new_func.called = False
8585
return new_func
86+
87+
88+
def is_namedtuple(value):
89+
# https://stackoverflow.com/a/2166841/1843746
90+
# But modified to handle subclasses of namedtuples.
91+
if not isinstance(value, tuple):
92+
return False
93+
f = getattr(type(value), '_fields', None)
94+
if not isinstance(f, tuple):
95+
return False
96+
return all(type(n) == str for n in f)

0 commit comments

Comments
 (0)