-
Notifications
You must be signed in to change notification settings - Fork 12
Enable strict typing with generics #438
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR enables stricter type checking by adding default type parameters to generic classes and making appropriate type parameters covariant. The changes improve type safety while maintaining backward compatibility through default parameter values.
Key changes:
- Adds
typing_extensions >= 4.4.0requirement for TypeVar defaults and Self type support - Enables mypy's
disallow_any_genericsstrict type checking - Refactors
StandardFootprintto use proper generic typing withSelftype - Removes legacy patterns (OrderedDict, ABCMeta, IR type parameter)
Reviewed changes
Copilot reviewed 50 out of 50 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| pyproject.toml | Updates typing_extensions version requirement and enables strict generic typing |
| edg/core/*.py | Adds default type parameters to core generics (TypeVars, BaseBlock, Link, Block) |
| edg/electronics_model/*.py | Parameterizes circuit-related generics and updates type annotations |
| edg/abstract_parts/StandardFootprint.py | Refactors to use Self type and adds lambda wrapper for circular dependencies |
| edg/abstract_parts/*.py | Updates component classes to use lambda wrapper for _STANDARD_FOOTPRINT and moves footprint definitions after component classes |
| edg/jlcparts/*.py | Adds RootModel type parameters for Pydantic models |
| examples/*.py | Adds type parameters to csv.DictWriter |
Comments suppressed due to low confidence (1)
edg/abstract_parts/StandardFootprint.py:27
- Variable name mismatch in assertion message. The f-string references
pinning_footprint(singular) but should referencepinning_footprints(plural) which is the actual variable being checked.
assert pinning_footprints not in footprint_map, f"duplicate footprint entry {pinning_footprint}"
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| PortTransformsType = Mapping[Type[Port], Tuple[Type[Port], Callable[[Port], Port]]] | ||
| DefaultPortTransforms: PortTransformsType = { | ||
| DigitalSource: (DigitalBidir, DigitalSource.from_bidir), | ||
| DigitalSink: (DigitalBidir, DigitalSink.from_bidir), | ||
| DigitalSource: (DigitalBidir, DigitalSource.from_bidir), # type: ignore | ||
| DigitalSink: (DigitalBidir, DigitalSink.from_bidir), # type: ignore |
Copilot
AI
Nov 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type: ignore comments suggest the type annotation for PortTransformsType may not accurately match the actual values. Consider whether the Callable type parameter should be more general (e.g., Callable[..., Port]) or if the dictionary values need adjustment to match the stricter type definition.
| def map_extract(self, selector: Callable[[VectorType], Union[ConstraintExpr, ExtractPortType]]) -> Union[ArrayExpr, DerivedVector[ExtractPortType]]: | ||
| param = selector(self._elt_sample) | ||
| if isinstance(param, ConstraintExpr): # TODO check that returned type is child | ||
| return ArrayExpr.array_of_elt(param)._bind(MapExtractBinding(self, param)) | ||
| elif isinstance(param, BasePort): | ||
| elif isinstance(param, Port): | ||
| return DerivedVector(self, param) | ||
| else: | ||
| raise EdgTypeError(f"selector return", param, (ConstraintExpr, BasePort)) | ||
| raise EdgTypeError(f"selector return", param, (ConstraintExpr, Port)) |
Copilot
AI
Nov 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type check on line 320 uses Port but the error message on line 323 also mentions Port. However, the union type at line 316 uses BasePort in the EdgTypeError elsewhere (see removed line). The isinstance check should be consistent with the type annotation - if the selector can return BasePort, the check should be isinstance(param, BasePort) not isinstance(param, Port).
|
|
||
|
|
||
| @non_library | ||
| class TableFerriteBead(PartsTableSelector, FerriteBead): |
Copilot
AI
Nov 28, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The _STANDARD_FOOTPRINT assignment that was previously at line 70 has been removed. This appears to be an unintended deletion rather than a deliberate change, as other similar table classes retain their footprint assignments. Verify if this is intentional or if it should inherit the lambda from the parent FerriteBead class.
| class TableFerriteBead(PartsTableSelector, FerriteBead): | |
| class TableFerriteBead(PartsTableSelector, FerriteBead): | |
| _STANDARD_FOOTPRINT = lambda: FerriteBeadStandardFootprint |
Adds default type parameters to generics to allow most code to work while the "default" generic class is a base class of sorts. This makes a lot of type parameters covariant.
The exception is Vector, which allows mutable elements, and remains invariant. BaseVector (non-parameterized) serves as the base class.
This also properly parameterizes the StandardFootprint type, so the block type in the lambdas are checked. HasStandardFootprint also now requires a StandardFootprint type-parameterzed on Self. To avoid circular dependencies, the _STANDARD_FOOTPRINT can be a lambda.
This uses typing_extensions for TypeVar defaults and Self for earlier version of Python.
Also removes OrderedDict and ABCMeta and the IR type parameter for Block.