diff options
| author | hippo91 <guillaume.peillex@gmail.com> | 2019-02-15 13:33:06 +0100 |
|---|---|---|
| committer | hippo91 <guillaume.peillex@gmail.com> | 2019-02-15 13:33:06 +0100 |
| commit | 2f1aec50a99f3155fd5ee840e2eed9eb044a279f (patch) | |
| tree | 56b92615fa6ce0444a05b5d7e532e9eb50b289cd | |
| parent | 6b5991d570a3743335816c5ecda0d07f5309a337 (diff) | |
| parent | 0bafc47c4fbe987e8dd1002d37bc3bcd62951405 (diff) | |
| download | astroid-git-2f1aec50a99f3155fd5ee840e2eed9eb044a279f.tar.gz | |
Merge branch 'master' of https://github.com/PyCQA/astroid
| -rw-r--r-- | ChangeLog | 6 | ||||
| -rw-r--r-- | astroid/brain/brain_namedtuple_enum.py | 8 | ||||
| -rw-r--r-- | astroid/interpreter/objectmodel.py | 4 | ||||
| -rw-r--r-- | astroid/tests/unittest_brain.py | 28 |
4 files changed, 43 insertions, 3 deletions
@@ -7,6 +7,12 @@ What's New in astroid 2.2.0? Release Date: TBA +* Fix a crash with ``typing.NamedTuple`` and empty fields. Close PyCQA/pylint#2745 + +* Add a proper ``strerror`` inference to the ``OSError`` exceptions. + + Close PyCQA/pylint#2553 + * Support non-const nodes as values of Enum attributes. Close #612 diff --git a/astroid/brain/brain_namedtuple_enum.py b/astroid/brain/brain_namedtuple_enum.py index 69e5eb8c..c50ef04a 100644 --- a/astroid/brain/brain_namedtuple_enum.py +++ b/astroid/brain/brain_namedtuple_enum.py @@ -178,7 +178,6 @@ def infer_named_tuple(node, context=None): attributes = _get_renamed_namedtuple_attributes(attributes) replace_args = ", ".join("{arg}=None".format(arg=arg) for arg in attributes) - field_def = ( " {name} = property(lambda self: self[{index:d}], " "doc='Alias for field number {index:d}')" @@ -412,9 +411,12 @@ def infer_typing_namedtuple(node, context=None): names.append(elt.elts[0].as_string()) typename = node.args[0].as_string() + if names: + field_names = "({},)".format(",".join(names)) + else: + field_names = "''" node = extract_node( - "namedtuple(%(typename)s, (%(fields)s,)) " - % {"typename": typename, "fields": ",".join(names)} + "namedtuple({typename}, {fields})".format(typename=typename, fields=field_names) ) return infer_named_tuple(node, context) diff --git a/astroid/interpreter/objectmodel.py b/astroid/interpreter/objectmodel.py index f031022f..581930dc 100644 --- a/astroid/interpreter/objectmodel.py +++ b/astroid/interpreter/objectmodel.py @@ -631,6 +631,10 @@ class OSErrorInstanceModel(ExceptionInstanceModel): def attr_errno(self): return node_classes.Const(0) + @property + def attr_strerror(self): + return node_classes.Const("") + attr_filename2 = attr_filename diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py index 10649f15..5badf037 100644 --- a/astroid/tests/unittest_brain.py +++ b/astroid/tests/unittest_brain.py @@ -1038,6 +1038,19 @@ class TypingBrain(unittest.TestCase): inferred = next(ast_node.infer()) assert isinstance(inferred, nodes.Tuple) + def test_typing_namedtuple_dont_crash_on_no_fields(self): + node = builder.extract_node( + """ + from typing import NamedTuple + + Bar = NamedTuple("bar", []) + + Bar() + """ + ) + inferred = next(node.infer()) + self.assertIsInstance(inferred, astroid.Instance) + class ReBrainTest(unittest.TestCase): def test_regex_flags(self): @@ -1840,5 +1853,20 @@ def test_http_client_brain(): assert isinstance(inferred, astroid.Instance) +def test_oserror_model(): + node = astroid.extract_node( + """ + try: + 1/0 + except OSError as exc: + exc #@ + """ + ) + inferred = next(node.infer()) + strerror = next(inferred.igetattr("strerror")) + assert isinstance(strerror, astroid.Const) + assert strerror.value == "" + + if __name__ == "__main__": unittest.main() |
