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

Generic PrimitiveJob and BaseSampler/BaseEstimator #9920

Merged
merged 4 commits into from
Apr 13, 2023

Conversation

ikkoham
Copy link
Contributor

@ikkoham ikkoham commented Apr 6, 2023

Summary

This PR improves type hinting with Generic.
The type of result returned by Primitives.run is made explicit, so it is enabling code completion and improving the user experience.

image

Details and comments

@ikkoham ikkoham added documentation Something is not clear or an error documentation Changelog: None Do not include in changelog mod: primitives Related to the Primitives module labels Apr 6, 2023
@ikkoham ikkoham added this to the 0.24.0 milestone Apr 6, 2023
@ikkoham ikkoham requested review from a team and t-imamichi as code owners April 6, 2023 02:53
@qiskit-bot
Copy link
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

One or more of the the following people are requested to review this:

@coveralls
Copy link

coveralls commented Apr 6, 2023

Pull Request Test Coverage Report for Build 4685190880

  • 15 of 15 (100.0%) changed or added relevant lines in 7 files are covered.
  • 2 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.006%) to 85.762%

Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 2 89.87%
Totals Coverage Status
Change from base Build 4681659057: 0.006%
Covered Lines: 70201
Relevant Lines: 81856

💛 - Coveralls

Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's useful to do this, but I don't think you need to override the derived methods to add the type hints. Deriving a class from Generic means you can define the specialisation during the inheritance, rather than at method resolution. For example, given a file generic.py:

import typing

T = typing.TypeVar("T")

class Base(typing.Generic[T]):
    def f(self) -> T:
        raise NotImplementedError

class Int(Base[int]):
    pass

class Str(Base[str]):
    pass

reveal_type(Base().f())
reveal_type(Int().f())
reveal_type(Str().f())

the result of running mypy on it is:

generic.py:15: note: Revealed type is "<nothing>"
generic.py:16: note: Revealed type is "builtins.int"
generic.py:17: note: Revealed type is "builtins.str"
Success: no issues found in 1 source file

@ikkoham
Copy link
Contributor Author

ikkoham commented Apr 6, 2023

@jakelishman Thanks. That's true, but to do so would require making JobV1 (that is return type of BaseEstimator/BaseSampler) Generic. I overrode the method instead of changing JobV1 , although it is a bit hacky.
Can I make JobV1 Generic?

@jakelishman
Copy link
Member

jakelishman commented Apr 6, 2023

Ah sorry, I'd misread the code a bit. Yeah, in the way the primitives typing is currently done, the way you've done things is the best. Making JobV1 generic wouldn't solve the problems here, because BaseEstimator and so on aren't generic, and they define the return value of run to be just Job, which has no methods defined. I think you'd either have to do what you've done and manually override the type hints, or the larger change is to also make BaseEstimator (etc) generic over a TypeVar("JobT", bound=Job) (I think it's covariant in this type too).

@ikkoham
Copy link
Contributor Author

ikkoham commented Apr 12, 2023

@jakelishman Thank you for your advice. I updated the code. Is this correct?

@ikkoham
Copy link
Contributor Author

ikkoham commented Apr 12, 2023

I believe #9480 will fix the lint error.

jakelishman
jakelishman previously approved these changes Apr 12, 2023
Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't comment on whether the type bounds you've chosen are precisely what the interface is meant to be (I assume that you're correct), but the implementation of them looks fine to me.

@jakelishman
Copy link
Member

I can re-approve once the conflicts are fixed. Looks like it's just super minor stuff.

@ikkoham
Copy link
Contributor Author

ikkoham commented Apr 13, 2023

@jakelishman Thank you very much! For the bound, this PR does not should neither strengthen nor weaken the restrictions of the type.

@ikkoham ikkoham changed the title Generic PrimitiveJob Generic PrimitiveJob and BaseSampler/BaseEstimator Apr 13, 2023
@jakelishman jakelishman added this pull request to the merge queue Apr 13, 2023
Merged via the queue into Qiskit:main with commit f2518cb Apr 13, 2023
@ikkoham ikkoham deleted the primitives/generic-job branch April 14, 2023 01:25
giacomoRanieri pushed a commit to giacomoRanieri/qiskit-terra that referenced this pull request Apr 16, 2023
* Generic PrimitiveJob

* Generic BaseSampler/BaseEstimator

* remove type hint in _run
king-p3nguin pushed a commit to king-p3nguin/qiskit-terra that referenced this pull request May 22, 2023
* Generic PrimitiveJob

* Generic BaseSampler/BaseEstimator

* remove type hint in _run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: None Do not include in changelog documentation Something is not clear or an error documentation mod: primitives Related to the Primitives module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants