Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

magicbot: Fix non-type annotation error for init #207

Merged
merged 1 commit into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions magicbot/inject.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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"