Skip to content
This repository has been archived by the owner on Feb 24, 2021. It is now read-only.

Missing recommended practice for replacing attribute with descriptor #27

Closed
mwchase opened this issue Sep 10, 2019 · 2 comments
Closed

Comments

@mwchase
Copy link
Owner

mwchase commented Sep 10, 2019

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.)

@mwchase
Copy link
Owner Author

mwchase commented Sep 10, 2019

(Anyone wondering about the presence of both Color and Colors, that is unrelated to this, and something I intend to address at some point.)

mwchase added a commit that referenced this issue Sep 11, 2019
@mwchase
Copy link
Owner Author

mwchase commented Oct 5, 2019

I did it.

@mwchase mwchase closed this as completed Oct 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant