Skip to content

Commit

Permalink
Merge pull request #207 from robotpy/magicbot-inject-init-non-type-msg
Browse files Browse the repository at this point in the history
magicbot: Fix non-type annotation error for init
  • Loading branch information
virtuald committed Nov 5, 2023
2 parents be4653f + a598e08 commit 5bcab9d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
8 changes: 5 additions & 3 deletions magicbot/inject.py
Expand Up @@ -39,10 +39,12 @@ def get_injection_requests(

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

requests[n] = inject_type

Expand Down
20 changes: 20 additions & 0 deletions tests/test_magicbot_inject.py
@@ -0,0 +1,20 @@
import typing

import pytest

from magicbot.inject import get_injection_requests


def test_ctor_invalid_type_hint_message():
"""
class Component:
def __init__(self, foo: 1): ...
"""
type_hints = {
"foo": typing.cast(type, 1),
}

with pytest.raises(TypeError) as exc_info:
get_injection_requests(type_hints, "bar")

assert exc_info.value.args[0] == "Component bar has a non-type annotation foo: 1"

0 comments on commit 5bcab9d

Please sign in to comment.