Skip to content

Commit 5bcab9d

Browse files
authored
Merge pull request #207 from robotpy/magicbot-inject-init-non-type-msg
magicbot: Fix non-type annotation error for init
2 parents be4653f + a598e08 commit 5bcab9d

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

magicbot/inject.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ def get_injection_requests(
3939

4040
# If the type is not actually a type, give a meaningful error
4141
if not isinstance(inject_type, type):
42-
raise TypeError(
43-
f"Component {cname} has a non-type annotation {n}: {inject_type}\n"
44-
"Lone non-injection variable annotations are disallowed, did you want to assign a static variable?"
42+
message = (
43+
f"Component {cname} has a non-type annotation {n}: {inject_type!r}"
4544
)
45+
if component is not None:
46+
message += "\nLone non-injection variable annotations are disallowed. Did you mean to assign a static variable?"
47+
raise TypeError(message)
4648

4749
requests[n] = inject_type
4850

tests/test_magicbot_inject.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import typing
2+
3+
import pytest
4+
5+
from magicbot.inject import get_injection_requests
6+
7+
8+
def test_ctor_invalid_type_hint_message():
9+
"""
10+
class Component:
11+
def __init__(self, foo: 1): ...
12+
"""
13+
type_hints = {
14+
"foo": typing.cast(type, 1),
15+
}
16+
17+
with pytest.raises(TypeError) as exc_info:
18+
get_injection_requests(type_hints, "bar")
19+
20+
assert exc_info.value.args[0] == "Component bar has a non-type annotation foo: 1"

0 commit comments

Comments
 (0)