Skip to content

Commit

Permalink
Fix type errors reported in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
bcmills committed Oct 15, 2024
1 parent 0a36a46 commit 1b9d3a1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ $(HATCH_BIN): $(VENV_TARGET)


# test suites
.PHONY: test test-examples test-unit
test: test-examples test-unit ## run all test suites
.PHONY: test test-examples test-unit test-types
test: test-examples test-unit test-types ## run all test suites
ALL_EXAMPLES := $(wildcard docs/examples/*.py)
test-examples: $(ALL_EXAMPLES) ## run example code test suite
.PHONY: $(ALL_EXAMPLES)
$(ALL_EXAMPLES): $(HATCH_BIN)
$(HATCH_CMD) $@
test-unit: $(VENV_TARGET) ## run unit test suite
$(HATCH_CMD) -m unittest discover -s tests
test-types:
$(PYTHON_CMD) -m hatch run types:check
14 changes: 9 additions & 5 deletions minject/inject_attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def inject_field(binding=_T, **attr_field_kwargs) -> Any:
# declaration. Extract the name of the field.
field_frame = stack[1]
name = ""
if len(field_frame.code_context) > 0:
if field_frame.code_context:
code = field_frame.code_context[0].strip()
name_and_type = code.split("=", maxsplit=1)[0].rstrip().lstrip()
name = name_and_type.split(":", maxsplit=1)[0].rstrip().lstrip()
Expand All @@ -122,9 +122,7 @@ def inject_field(binding=_T, **attr_field_kwargs) -> Any:
# bindings, so double-check that that assumption holds.
# (If not, our inferred field name is probably wrong too!)
class_frame = stack[2]
if len(class_frame.code_context) < 1 or not class_frame.code_context[0].strip().startswith(
"class "
):
if not (class_frame.code_context and class_frame.code_context[0].strip().startswith("class ")):
raise ValueError(
"Could not find line containing class declaration. Are you calling inject_field properly?"
)
Expand Down Expand Up @@ -158,9 +156,15 @@ def inject_define_inner(cls: Type[_P]) -> Type[_P]:
"Could not find line containing class declaration. Are you calling inject_define properly?"
)

class_filename = inspect.getsourcefile(cls)
if class_filename is None:
raise ValueError(
"Could not find filename of class declaration. Are you calling inject_define properly?"
)

# get bindings to apply to the class
key = _BindingKey(
filename=inspect.getsourcefile(cls),
filename=class_filename,
class_lineno=class_lineno,
)
bindings = _key_binding_mapping[key]
Expand Down

0 comments on commit 1b9d3a1

Please sign in to comment.