Skip to content

fix: Fix add_component() type error with Mypy - #9344

Closed
denisw wants to merge 1 commit into
deepset-ai:mainfrom
denisw:mypy-run-read-only
Closed

fix: Fix add_component() type error with Mypy#9344
denisw wants to merge 1 commit into
deepset-ai:mainfrom
denisw:mypy-run-read-only

Conversation

@denisw

@denisw denisw commented May 5, 2025

Copy link
Copy Markdown
Contributor

Related Issues

None, though some # type: ignore comments in the codebase demonstrate that the core team hit this issue. :)

Proposed Changes:

When calling pipeline.add_component() with any component instance, Mypy complains:

error: Argument 2 to "add_component" of "PipelineBase" has incompatible type "X"; expected "Component"  [arg-type]
note: Protocol member Component.run expected settable variable, got read-only attribute

The issue is that run of the Component protocol is currently defined as a mutable attribute (run: ...). Use @property instead to only require a getter.

How did you test it?

I ran the type tests with hatch run test:types after making the change.

Notes for the reviewer

An additional improvement - and my original goal before I got side-tracked by this - would be to change the required return type of run() from Dict to Mapping so that it becomes possible to define a run() return type as a TypedDict. I will create a separate follow-up PR for this, though.

Checklist

  • I have read the contributors guidelines and the code of conduct
  • I have updated the related issue with new insights and changes
  • I added unit tests and updated the docstrings
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I documented my code
  • I ran pre-commit hooks and fixed any issue

@denisw
denisw requested a review from a team as a code owner May 5, 2025 21:02
@denisw
denisw requested review from davidsbatista and removed request for a team May 5, 2025 21:02
@CLAassistant

CLAassistant commented May 5, 2025

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

When calling `pipeline.add_component()` with any component instance,
Mypy complains:

```
error: Argument 2 to "add_component" of "PipelineBase" has incompatible type "X"; expected "Component"  [arg-type]
note: Protocol member Component.run expected settable variable, got read-only attribute
```

The issue is that `run` of the `Component` protocol is currently
defined as a mutable attribute (`run: ...`). Use `@property` instead
to only require a getter.
@denisw
denisw force-pushed the mypy-run-read-only branch from 8d11762 to e1795ed Compare May 5, 2025 21:29
@coveralls

Copy link
Copy Markdown
Collaborator

Pull Request Test Coverage Report for Build 14846645043

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage increased (+0.001%) to 90.411%

Files with Coverage Reduction New Missed Lines %
core/component/component.py 1 99.45%
Totals Coverage Status
Change from base Build 14838765775: 0.001%
Covered Lines: 10909
Relevant Lines: 12066

💛 - Coveralls

@denisw

denisw commented May 6, 2025

Copy link
Copy Markdown
Contributor Author

Closing as this is already addressed by #9329.

@denisw denisw closed this May 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants