From 1b9d3a12f3868b9a7fcc5145ad2d69929741dcb6 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Tue, 15 Oct 2024 14:45:29 -0400 Subject: [PATCH] Fix type errors reported in CI --- Makefile | 6 ++++-- minject/inject_attrs.py | 14 +++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 774d2c8..ade499f 100644 --- a/Makefile +++ b/Makefile @@ -28,8 +28,8 @@ $(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) @@ -37,3 +37,5 @@ $(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 diff --git a/minject/inject_attrs.py b/minject/inject_attrs.py index fe1c1a3..427e195 100644 --- a/minject/inject_attrs.py +++ b/minject/inject_attrs.py @@ -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() @@ -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?" ) @@ -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]