Open
Description
This issue is a place to discuss the convention for attributes initialized as None specifically in the situation where:
- These attributes are not specified using arguments passed in
__init__
- These attributes cannot actually be used as type
None
and have to be assigned. - The attribute is never used in the form
if self.attribute is None: ...
For concreteness, let's focus on a specific example of this. In the current Petting Zoo implementation we have:
class Entity: # properties and state of physical world entity
def __init__(self):
...
self.max_speed = None
...
and then, in Simple Tag:
def make_world(self, num_good=1, num_adversaries=3, num_obstacles=2):
...
world.agents = [Agent() for i in range(num_agents)]
for i, agent in enumerate(world.agents):
agent.adversary = True if i < num_adversaries else False
...
agent.max_speed = 1.0 if agent.adversary else 1.3
...
Possible Proposals
- Do nothing (requires
# pyright:ignore
because type of attribute is inferred asNone
.). - In the definition of
Entity
change to:
class Entity: # properties and state of physical world entity
def __init__(self):
...
self.max_speed : bool | None = None
...
- In the definition of
Entity
change to:
class Entity: # properties and state of physical world entity
def __init__(self):
...
@property
def max_speed(self):
if self._max_speed is None:
raise NotImplementedError
return self._max_speed
@max_speed.setter
def max_speed(self, val : float):
self._max_speed = val
- Something else.
In the most recent PR (#1 ), I took option 3, but I don't have an especially strong stance.
Would love to hear any thoughts folks might have on this!
Metadata
Metadata
Assignees
Labels
No labels