This repository was archived by the owner on Feb 24, 2021. It is now read-only.
This repository was archived by the owner on Feb 24, 2021. It is now read-only.
Missing recommended practice for replacing attribute with descriptor #27
Closed
Description
Blocked on python/mypy#4125
Motivation:
Dennis contains the following Coconut code
class Sprite:
__slots__ = ()
char: int
name_: str
color: colors.Color
render_order: RenderOrder
data ConcreteSprite(char: int, name_: str, color: colors.Color, render_order: RenderOrder) from Sprite
class Alive(ConcreteSprite, enum.Enum):
PLAYER = ConcreteSprite(ord("@"), "Player", colors.Colors.PLAYER, RenderOrder.ACTOR)
ORC = ConcreteSprite(ord("o"), "Orc", colors.Colors.ORC, RenderOrder.ACTOR)
TROLL = ConcreteSprite(ord("T"), "Troll", colors.Colors.TROLL, RenderOrder.ACTOR)
data Dead(corpse: Alive) from Sprite:
char = ord("%")
@property
def name_(self) -> str: # type: ignore
corpse_name = self.corpse.name_
if self.corpse != Alive.PLAYER:
corpse_name = f"remains of {corpse_name}"
return corpse_name
color = colors.Colors.DEAD
@property
def render_order(self) -> RenderOrder: # type: ignore
if self.corpse == Alive.PLAYER:
return RenderOrder.ACTOR
return RenderOrder.CORPSE
Translated to pure-Python with Structured Data, it should be:
class Sprite:
__slots__ = ()
char: int
name_: str
color: colors.Color
render_order: RenderOrder
class ConcreteSprite(Sprite, adt.Product):
__slots__ = ()
# Can I switch this to just using Sprite?
# Might be blocked on https://github.com/mwchase/python-structured-data/issues/23
class Alive(ConcreteSprite, enum.Enum):
PLAYER = ConcreteSprite(ord("@"), "Player", colors.Colors.PLAYER, RenderOrder.ACTOR)
ORC = ConcreteSprite(ord("o"), "Orc", colors.Colors.ORC, RenderOrder.ACTOR)
TROLL = ConcreteSprite(ord("T"), "Troll", colors.Colors.TROLL, RenderOrder.ACTOR)
class Dead(Sprite, adt.Product):
corpse: Alive
char: typing.ClassVar[int] = ord("%")
name_: ???
color: typing.ClassVar[colors.Color] = colors.Colors.DEAD
render_order: ???
@property
def name_(self) -> str:
corpse_name = self.corpse.name_
if self.corpse != Alive.PLAYER:
corpse_name = f"remains of {corpse_name}"
return corpse_name
@property
def render_order(self) -> RenderOrder:
if self.corpse == Alive.PLAYER:
return RenderOrder.ACTOR
return RenderOrder.CORPSE
(Maybe with some more __slots__ = ()
in there.)
Metadata
Metadata
Assignees
Labels
No labels