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

619 move to pyright #632

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

619 move to pyright #632

wants to merge 11 commits into from

Conversation

stan-dot
Copy link
Contributor

@stan-dot stan-dot commented Sep 11, 2024

points that need addressing

  • sim_motor types issues - might ignore but alternatively just delete
  • task worker
    • state is also problematic
    • 'run', the part calling the RunEngine with the hooks is unclear

@stan-dot stan-dot added documentation Improvements or additions to documentation dependencies Pull requests that update a dependency file python Pull requests that update Python code dev-environment Issues for improving the developer environment/experience labels Sep 11, 2024
@stan-dot stan-dot self-assigned this Sep 11, 2024
@stan-dot stan-dot linked an issue Sep 11, 2024 that may be closed by this pull request
@stan-dot
Copy link
Contributor Author

stan-dot commented Oct 4, 2024

@callumforrester please let me know your thoughts

@callumforrester
Copy link
Contributor

@stan-dot I like where this is going, thanks

@stan-dot
Copy link
Contributor Author

ok will try to finish this today

@stan-dot
Copy link
Contributor Author

simmotor has float and has that in the tests as well - but the Ophyd type is this: value : object, optional The initial value. Default is 0. which pyright reads as int.

adding an ignore line pending ophyd choosing a side here

@stan-dot
Copy link
Contributor Author

task_worker complains - because run_engine is not typed 😢

@stan-dot
Copy link
Contributor Author

DeviceStatus in devices.py complains that the DeviceStatus (imported from ophyd ❗ ) does not support the bluesky Status protocol

@stan-dot
Copy link
Contributor Author

still one issue with the stomp instantiation from the CLI - what's your opinion @callumforrester ?

@stan-dot stan-dot marked this pull request as ready for review October 11, 2024 09:59
@stan-dot
Copy link
Contributor Author

src/blueapi/service/interface.py Outdated Show resolved Hide resolved
src/blueapi/service/runner.py Outdated Show resolved Hide resolved
src/blueapi/startup/simmotor.py Show resolved Hide resolved
src/blueapi/utils/__init__.py Show resolved Hide resolved
@@ -95,7 +95,7 @@ def __init__(

self._tasks = {}

self._state = WorkerState.from_bluesky_state(ctx.run_engine.state)
self._state = WorkerState.from_bluesky_state(ctx.run_engine.state) # type: ignore
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems fixable by setting the return type of from_bluesky_state, possibly including a cast?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will look into it

@@ -181,8 +182,8 @@ def mark_task_as_started(event: WorkerEvent, _: str | None) -> None:
task_started.set()

LOGGER.info(f"Submitting: {trackable_task}")
sub = self.worker_events.subscribe(mark_task_as_started)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should: Moving this out of the try block means a network error could potentially bring down the worker subprocess

Copy link
Contributor Author

Choose a reason for hiding this comment

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

from the first look on it, to add a callback on a potential event is not a network event.

it is only the following line:

            self._task_channel.put_nowait(trackable_task)
            

that does something that looks like network logic

Copy link
Contributor

Choose a reason for hiding this comment

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

That's not network logic, that's adding to an internal queue. However, it is not just network logic you need to worry about: Anything that can throw an exception should go inside the try block, since we don't want the subprocess to crash and leave the main process spinning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

from the first look on it, to add a callback on a potential event is not a network event.

as I expected. if the first look was wrong, it means that the variable and file naming is not descriptive enough...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

why adding to an internal queue would fail?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a comment explaining that part.

Still I think the discussion got sidetracked a bit as

    sub = self.worker_events.subscribe(mark_task_as_started)

is not the put_nowait line

Copy link
Contributor

Choose a reason for hiding this comment

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

You're right, I don't think that line could cause a network error, but it could still raise an exception, so why was it moved out of the try block?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

because it's just adding a callback

Copy link
Contributor

Choose a reason for hiding this comment

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

So if anything in _cycle raises an uncaught error it will kill the worker thread and blueapi will need a manual restart, which is why I prefer to play it safe and just catch and log any exception in the cycle loop. That code won't currently raise an exception, but it could be modified in the future such that it does, and the person modifying it won't necessarily know about this line. Please put it back.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, I'll include information from your comment for clarity

tests/unit_tests/worker/devices.py Show resolved Hide resolved
@callumforrester
Copy link
Contributor

@stan-dot: Maybe we should make the password a secret in a separate PR, in order to deal with the errors. It is not essential to adopting pyright.

@stan-dot
Copy link
Contributor Author

stan-dot commented Oct 14, 2024

I'd agree with you, @callumforrester had this difference was between a 200 lines PR and 1k lines PR. it's 100 +- lines at the moment so easy to review. :( The errors are from pyright, so it makes sense to make this as a change

Maybe we should make the password a secret in a separate PR

this soft maybe does not feel honest as we both know you'd abosolutely not allow for this to go in the same PR, so please skip the pleasantries and just tell me to raise an issue and make a PR...

@callumforrester
Copy link
Contributor

For the record it was a genuine maybe, I agree that those changes are tangentially related. I read them during review and decided not to contest them unless they caused problems. I am unable to commit the time to help you debug the error right now so I'm leaving it up to you whether you want to put time into debugging on your own or make an issue and defer until later. I am happy with either resolution.

@stan-dot
Copy link
Contributor Author

  /workspaces/blueapi/src/blueapi/startup/simmotor.py:41:9 - error: Method "set" overrides class "SynAxis" in an incompatible manner
    Return type mismatch: base method returns type "MoveStatus", override returns type "Status"
      "Status" is not assignable to "MoveStatus" (reportIncompatibleMethodOverride)
  /workspaces/blueapi/src/blueapi/startup/simmotor.py:70:14 - error: "MoveStatus" is not defined (reportUndefinedVariable)
  /workspaces/blueapi/src/blueapi/startup/simmotor.py:93:36 - error: "MoveStatus" is not defined (reportUndefinedVariable)
  /workspaces/blueapi/src/blueapi/startup/simmotor.py:94:16 - error: "MoveStatus" is not defined (reportUndefinedVariable)
/workspaces/blueapi/src/blueapi/worker/task_worker.py
  /workspaces/blueapi/src/blueapi/worker/task_worker.py:99:54 - error: Argument of type "LoggingPropertyMachine | ProxyString | str" cannot be assigned to parameter "bluesky_state" of type "RawRunEngineState" in function "from_bluesky_state"
    Type "LoggingPropertyMachine | ProxyString | str" is not assignable to type "RawRunEngineState"
      Type "str" is not assignable to type "RawRunEngineState"
        Type "str" is not assignable to type "type[ProxyString]"
        Type "str" is not assignable to type "type[str]"
        Type "str" is not assignable to type "type[PropertyMachine]" (reportArgumentType)
/workspaces/blueapi/tests/unit_tests/worker/devices.py
  /workspaces/blueapi/tests/unit_tests/worker/devices.py:43:9 - error: Method "set" overrides class "Movable" in an incompatible manner
    Return type mismatch: base method returns type "Status", override returns type "Status"
      "Status" is incompatible with protocol "Status"
        "exception" is an incompatible type
          Type "(timeout: Unknown | None = None) -> (StatusTimeoutError | Exception | type[Exception] | None)" is not assignable to type "(timeout: float | None = 0) -> (BaseException | None)"
            Function return type "StatusTimeoutError | Exception | type[Exception] | None" is incompatible with type "BaseException | None"
        "bluesky.protocols.Status" is not assignable to "ophyd.status.Status"
        "bluesky.protocols.Status" is not assignable to "ophyd.status.Status" (reportIncompatibleMethodOverride)
  /workspaces/blueapi/tests/unit_tests/worker/devices.py:45:16 - error: Type "AdditionalUpdateStatus" is not assignable to return type "Status"
    "AdditionalUpdateStatus" is not assignable to "Status" (reportReturnType)
7 errors, 0 warnings, 0 informations 

those are the errors, they are all grounded in the chaos around ophyd-vs-ophyd-async bluesky protocol import (SynAxis and worker devices cases), as well as with State tracking - EngineState and the application specific state, and the unexpected super_state_machine import - not maintained, afaik vendored in into Bluesky.

either those are solved in a different PR or this PR is merged with 'ignore' flags.

@callumforrester
Copy link
Contributor

@stan-dot I am okay with ignore flags on ophyd (not ophyd-async) devices, this problem is probably related to bluesky/bluesky#1809

@stan-dot
Copy link
Contributor Author

any idea why are the tests taking forever @callumforrester ?

@callumforrester
Copy link
Contributor

callumforrester commented Oct 17, 2024

No although all bets are off while main is broken...
They may have been queued, we can only run up to 20 in the org at once

@stan-dot
Copy link
Contributor Author

will just make new issues for the type: ignores. also a propagation of changes to upstream repos to increase type safety there

@stan-dot
Copy link
Contributor Author

locally tests also take a long time, looking for which one makes it so long

@stan-dot
Copy link
Contributor Author

also issues on main

@callumforrester
Copy link
Contributor

@stan-dot is #601 related to the test issues?

@stan-dot
Copy link
Contributor Author

not sure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file dev-environment Issues for improving the developer environment/experience documentation Improvements or additions to documentation python Pull requests that update Python code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move to pyright - awaits dodal change
3 participants