Skip to content

Commit 9f293a6

Browse files
authored
Merge pull request #83 from auscompgeek/magicbot-injection-tests
Add more tests for variable annotation injection
2 parents 1ba9260 + 1cc621f commit 9f293a6

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ Authors
4444

4545
- Dustin Spicuzza (dustin@virtualroadside.com)
4646
- Tim Winters (twinters@wpi.edu)
47+
- David Vo (@auscompgeek)
4748
- Insert your name here!

magicbot/magicrobot.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -456,16 +456,17 @@ def _create_components(self):
456456
# raise an error. No double declaring types, e.g foo: type = type
457457
if isinstance(attr, type) and not isinstance(attr, property):
458458
raise ValueError("%s.%s has two type declarations" % (cls.__name__, m))
459+
# Otherwise, skip this set class variable
459460
continue
460461

461-
# If the class variable has been assigned in __init__, skip it
462+
# If the variable has been assigned in __init__ or createObjects, skip it
462463
if hasattr(self, m):
463464
continue
464465

465466
# If the type is not actually a type, give a meaningful error
466467
if not isinstance(ctyp, type):
467-
raise TypeError('%s has a non-type annotation on %s (%s); lone non-injection variable annotations are disallowed, did you want to assign a static variable?' %
468-
(cls.__name__, m, ctyp))
468+
raise TypeError('%s has a non-type annotation on %s (%r); lone non-injection variable annotations are disallowed, did you want to assign a static variable?'
469+
% (cls.__name__, m, ctyp))
469470

470471
component = self._create_component(m, ctyp)
471472

@@ -531,7 +532,7 @@ def _create_component(self, name, ctyp):
531532

532533
# Ensure that mandatory methods are there
533534
if not callable(getattr(component, 'execute', None)):
534-
raise ValueError("Component %s (%s) must have a method named 'execute'" % (name, component))
535+
raise ValueError("Component %s (%r) must have a method named 'execute'" % (name, component))
535536

536537
# Automatically inject a logger object
537538
component.logger = logging.getLogger(name)
@@ -560,16 +561,17 @@ def _setup_vars(self, cname, component):
560561
# raise an error. No double declaring types, e.g foo: type = type
561562
if isinstance(attr, type) and not isinstance(attr, property):
562563
raise ValueError("%s.%s has two type declarations" % (component_type.__name__, n))
564+
# Otherwise, skip this set class variable
563565
continue
564566

565-
# If the class variable has been assigned in __init__, skip it
567+
# If the variable has been assigned in __init__, skip it
566568
if hasattr(component, n):
567569
continue
568570

569571
# If the type is not actually a type, give a meaningful error
570572
if not isinstance(inject_type, type):
571-
raise TypeError('Component %s has a non-type annotation on %s (%s); lone non-injection variable annotations are disallowed, did you want to assign a static variable?' %
572-
(cname, n, inject_type))
573+
raise TypeError('Component %s has a non-type annotation on %s (%r); lone non-injection variable annotations are disallowed, did you want to assign a static variable?'
574+
% (cname, n, inject_type))
573575

574576
self._inject(n, inject_type, cname, component)
575577

tests/magicbot_annotation_tester.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,31 @@ class InheritedBot(BotBase):
9898

9999
class TypeHintedBot(magicbot.MagicRobot):
100100
some_int: int = 1
101+
some_float: float
101102

102103
component: DumbComponent
103104

104105
def createObjects(self):
105-
pass
106+
self.some_float = 0.5
106107

107108

108109
class TypeHintedComponent:
109110
injectable: Injectable
110111

111112
some_int: int = 1
113+
maybe_float: float = None
114+
calculated_num: float
115+
116+
def __init__(self):
117+
self.calculated_num = 1 - self.some_int
112118

113119
def execute(self):
114120
pass
115121

116122

117123
class TypeHintsBot(magicbot.MagicRobot):
124+
injectable: Injectable
125+
118126
component: TypeHintedComponent
119127

120128
def createObjects(self):

tests/test_magicbot_injection.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ def test_typehintedbot():
182182

183183
assert isinstance(bot.component, DumbComponent)
184184
assert bot.some_int == 1
185+
assert bot.some_float == 0.5
185186

186187
def test_typehints_inject():
187188
from magicbot_annotation_tester import TypeHintsBot, TypeHintedComponent, Injectable

0 commit comments

Comments
 (0)