]> Devi Nivas Git - chevron.git/commitdiff
Use getattr instead of _asdict/__getitem__
authorMax Bernstein <emacs@fb.com>
Sat, 2 Jan 2021 18:19:19 +0000 (10:19 -0800)
committerMax Bernstein <emacs@fb.com>
Sat, 2 Jan 2021 18:19:19 +0000 (10:19 -0800)
`_asdict` is not public API for `namedtuple`.

You may want to consider using the built-in `string` module, which
implements this kind of `.0`/`.foo` formatting.

chevron/renderer.py

index 56a55bf6e417afe0858a190bf04a5cf2e870c3fb..50d2e32641fd5cb58ca91f1a8f4ea6fd93ea3d1b 100644 (file)
@@ -67,16 +67,10 @@ def _get_key(key, scopes, warn=False):
                     scope = scope[child]
                 except (TypeError, AttributeError):
                     try:
-                        # Try namedtuple (which does not have __dict__ in
-                        # Python 3: https://bugs.python.org/issue24931)
-                        scope = scope._asdict()[child]
+                        scope = getattr(scope, child)
                     except (TypeError, AttributeError):
-                        try:
-                            # Try the dictionary (Complex types)
-                            scope = scope.__dict__[child]
-                        except (TypeError, AttributeError):
-                            # Try as a list
-                            scope = scope[int(child)]
+                        # Try as a list
+                        scope = scope[int(child)]
 
             # Return an empty string if falsy, with two exceptions
             # 0 should return 0, and False should return False