Skip to content

Rudimentary support for Enum #715

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

Merged
merged 4 commits into from
Aug 10, 2015
Merged
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
3 changes: 3 additions & 0 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ def analyse_class_attribute_access(itype: Instance,
if isinstance(node.node, TypeInfo):
msg.fail(messages.CANNOT_ASSIGN_TO_TYPE, context)

if itype.type.is_enum and not (is_lvalue or is_decorated or is_method):
return itype

t = node.type
if t:
is_classmethod = is_decorated and cast(Decorator, node.node).func.is_class
Expand Down
1 change: 1 addition & 0 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1398,6 +1398,7 @@ class TypeInfo(SymbolNode):
names = None # type: SymbolTable # Names defined directly in this type
is_abstract = False # Does the class have any abstract attributes?
abstract_attributes = None # type: List[str]
is_enum = False

# Information related to type annotations.

Expand Down
2 changes: 2 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,8 @@ def analyze_base_classes(self, defn: ClassDef) -> None:
defn.base_types.append(base)
elif not isinstance(base, UnboundType):
self.fail('Invalid base class', base_expr)
if isinstance(base, Instance):
defn.info.is_enum = base.type.fullname() == 'enum.Enum'
# Add 'object' as implicit base if there is no other base class.
if (not defn.base_types and defn.fullname != 'builtins.object'):
obj = self.object_type()
Expand Down
18 changes: 18 additions & 0 deletions mypy/test/data/pythoneval-enum.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- Test cases for type checking mypy programs using full stubs and running
-- using CPython.
--
-- These are mostly regression tests -- no attempt is made to make these
-- complete.
--
-- This test file checks Enum

[case testEnum]
from enum import Enum
class Medal(Enum):
gold = 1
silver = 2
bronze = 3
m = Medal.gold
m = 1
[out]
_program.py, line 7: Incompatible types in assignment (expression has type "int", variable has type "Medal")
3 changes: 2 additions & 1 deletion mypy/test/testpythoneval.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
python_eval_files = ['pythoneval.test',
'python2eval.test']

python_34_eval_files = ['pythoneval-asyncio.test']
python_34_eval_files = ['pythoneval-asyncio.test',
'pythoneval-enum.test']

# Path to Python 3 interpreter
python3_path = sys.executable
Expand Down
13 changes: 0 additions & 13 deletions stubs/3.2/enum.pyi

This file was deleted.

24 changes: 24 additions & 0 deletions stubs/3.4/enum.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from typing import List, Any, TypeVar


class Enum:
def __new__(cls, value: Any) -> None: ...
def __repr__(self) -> str: ...
def __str__(self) -> str: ...
def __dir__(self) -> List[str]: ...
def __format__(self, format_spec: str) -> str: ...
def __hash__(self) -> Any: ...
def __reduce_ex__(self, proto: Any) -> Any: ...

def name(self) -> str: ...
def value(self) -> Any: ...


class IntEnum(int, Enum):
pass


_T = TypeVar('_T')

def unique(enumeration: _T) -> _T:
pass