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

Put pyright in strict mode #143

Merged
merged 6 commits into from
Sep 16, 2024
Merged

Put pyright in strict mode #143

merged 6 commits into from
Sep 16, 2024

Conversation

callumforrester
Copy link
Contributor

@callumforrester callumforrester commented Sep 13, 2024

Fixes #144

@callumforrester callumforrester changed the title Pyright strict Put pyright in strict mode Sep 13, 2024
@coretl
Copy link
Contributor

coretl commented Sep 13, 2024

#142 fixes schema

from pydantic import BaseModel, ConfigDict, Field, GetCoreSchemaHandler
from pydantic.dataclasses import is_pydantic_dataclass, rebuild_dataclass
from pydantic_core import CoreSchema
from pydantic_core.core_schema import tagged_union_schema

__all__ = [
"if_instance_do",
"Axis",
Copy link
Contributor

Choose a reason for hiding this comment

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

This was in __all__ so the docs didn't complain about a missing ref, please can you check the docs still look ok after this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since I agree with #143 (comment), I'm putting this back, however I was planning to manually check the docs and the plotting once you were happy with everything else, so will leave this thread open as a placeholder for that.

@@ -32,6 +33,8 @@
"find_regions",
]

NpMask = npt.NDArray[np.bool]
Copy link
Contributor

Choose a reason for hiding this comment

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

BTW I recently discovered you can specify shape too, although most of numpy returns shape=Any so it's of limited use at the mo:

ScalarType_co = TypeVar("ScalarType_co", bound=np.generic, covariant=True)
OneDArray = np.ndarray[tuple[int], np.dtype[ScalarType_co]]
TwoDArray = np.ndarray[tuple[int, int], np.dtype[ScalarType_co]]

NPMask = OneDArray[np.bool]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting... worth a separate issue to specify shape where possible?

Comment on lines 45 to 53
#: A type variable for an `axis_` that can be specified for a scan
Axis = TypeVar("Axis", covariant=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why re-implement rather than import?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My mistake, I thought it was best practice, but having researched a bit more I agree that importing TypeVars is the way to go. Case in point: When I removed these it broke because Axis in core.py wasn't covariant. I have now fixed and imported both variables.

def __mul__(self, other: Spec[Axis]) -> Product[Axis]: ...

@overload
def __mul__(self, other: Spec[OtherAxis]) -> Product[Axis | OtherAxis]: ...
Copy link
Contributor

Choose a reason for hiding this comment

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

It's variance like this that makes me think duration should be special, otherwise we end up with lots of Spec[Motor | str] and isinstance checks.

Alternatively we might decide that we always need Spec[str] with a lookup, as the case for Spec[Motor] in ophyd_async is looking tenuous. One for the scanspec v1 ticket (I realised we never made scanspec 1.0 so might as well use that number).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A few thoughts here:

  • Spec[Motor] would be very nice for blueapi, if we can get it to work with pydantic, even Spec[Motor, str].
  • If we make time special there might be a way to incorporate it into a Spec[Motor] without making it a Spec[Motor, str] (or really a Spec[Motor, Time], which would be preferable)
  • However we should ask ourselves if the variance is actually a bad thing, the extra verbosity here is just capturing a special case at "compile" time rather than runtime, the special case always existed and was always special, strict mode simply forces us to acknowledge it. In other words it is possible that a bunch of isinstance checks really might be the right thing to do here, or it's possible that we can box the specialness into one place by treating time as special somehow. I am not sure, but I don't think we should be prescriptive.

Copy link

codecov bot commented Sep 16, 2024

Codecov Report

Attention: Patch coverage is 96.15385% with 6 lines in your changes missing coverage. Please review.

Project coverage is 95.74%. Comparing base (e42f8eb) to head (b49ed30).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/scanspec/sphinxext.py 44.44% 5 Missing ⚠️
src/scanspec/service.py 90.90% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #143      +/-   ##
==========================================
- Coverage   95.89%   95.74%   -0.15%     
==========================================
  Files           9        9              
  Lines         925      940      +15     
==========================================
+ Hits          887      900      +13     
- Misses         38       40       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@coretl coretl left a comment

Choose a reason for hiding this comment

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

Happy for you to merge when you've checked the docs

@callumforrester callumforrester merged commit e1220f5 into main Sep 16, 2024
20 of 21 checks passed
@callumforrester callumforrester deleted the pyright-strict branch September 16, 2024 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enable Strict Mode for Pyright
2 participants