diff options
| author | Claudiu Popa <pcmanticore@gmail.com> | 2018-11-29 15:49:27 +0100 |
|---|---|---|
| committer | Claudiu Popa <pcmanticore@gmail.com> | 2018-11-29 15:49:27 +0100 |
| commit | 9f0b404cacee3606fecae7243fe8bab6c7bde3fa (patch) | |
| tree | 85c5ab1b423d19d4a255f754b65315f3aa9def68 | |
| parent | 7799c725ec0072d34d3dce2bf16a78cd46951dd0 (diff) | |
| download | astroid-git-9f0b404cacee3606fecae7243fe8bab6c7bde3fa.tar.gz | |
Added special support for `enum.IntFlag`
Close PyCQA/pylint#2534
| -rw-r--r-- | ChangeLog | 9 | ||||
| -rw-r--r-- | astroid/__pkginfo__.py | 2 | ||||
| -rw-r--r-- | astroid/brain/brain_namedtuple_enum.py | 34 |
3 files changed, 43 insertions, 2 deletions
@@ -2,6 +2,15 @@ astroid's ChangeLog =================== +What's New in astroid 2.2.0? +============================ +Release Date: TBA + +* Added special support for `enum.IntFlag` + + Close PyCQA/pylint#2534 + + What's New in astroid 2.1.0? ============================ Release Date: 2018-11-25 diff --git a/astroid/__pkginfo__.py b/astroid/__pkginfo__.py index 4ab67567..efcd1d67 100644 --- a/astroid/__pkginfo__.py +++ b/astroid/__pkginfo__.py @@ -21,7 +21,7 @@ distname = "astroid" modname = "astroid" -version = "2.1.0" +version = "2.2.0" numversion = tuple(int(elem) for elem in version.split(".") if elem.isdigit()) extras_require = {} diff --git a/astroid/brain/brain_namedtuple_enum.py b/astroid/brain/brain_namedtuple_enum.py index 44a41c42..6dc07f5e 100644 --- a/astroid/brain/brain_namedtuple_enum.py +++ b/astroid/brain/brain_namedtuple_enum.py @@ -30,7 +30,14 @@ from astroid import util TYPING_NAMEDTUPLE_BASENAMES = {"NamedTuple", "typing.NamedTuple"} -ENUM_BASE_NAMES = {"Enum", "IntEnum", "enum.Enum", "enum.IntEnum"} +ENUM_BASE_NAMES = { + "Enum", + "IntEnum", + "enum.Enum", + "enum.IntEnum", + "IntFlag", + "enum.IntFlag", +} def _infer_first(node, context): @@ -270,6 +277,24 @@ def infer_enum(node, context=None): return iter([class_node.instantiate_class()]) +INT_FLAG_ADDITION_METHODS = """ + def __or__(self, other): + return {name}(self.value | other.value) + def __and__(self, other): + return {name}(self.value & other.value) + def __xor__(self, other): + return {name}(self.value ^ other.value) + def __add__(self, other): + return {name}(self.value + other.value) + def __div__(self, other): + return {name}(self.value / other.value) + def __invert__(self): + return {name}(~self.value) + def __mul__(self, other): + return {name}(self.value * other.value) +""" + + def infer_enum_class(node): """ Specific inference for enums. """ for basename in node.basenames: @@ -319,6 +344,13 @@ def infer_enum_class(node): return_value=inferred_return_value, ) ) + if "IntFlag" in basename: + # Alright, we need to add some additional methods. + # Unfortunately we still can't infer the resulting objects as + # Enum members, but once we'll be able to do that, the following + # should result in some nice symbolic execution + classdef += INT_FLAG_ADDITION_METHODS.format(name=target.name) + fake = AstroidBuilder(MANAGER).string_build(classdef)[target.name] fake.parent = target.parent for method in node.mymethods(): |
