Skip to content

Various mypy related updates - #55255

Merged
adk3798 merged 11 commits into
ceph:mainfrom
phlogistonjohn:jjm-mypy-updates
Jan 29, 2024
Merged

adk3798 merged 11 commits into
ceph:mainfrom
phlogistonjohn:jjm-mypy-updates

Conversation

@phlogistonjohn

Copy link
Copy Markdown
Contributor

While working on one task I ran into some mypy related issue that I thought may be fixed in newer versions of mypy. But I was not easily able to try a newer version because the version is pinned in a "global" mypy constraints file - and updating the version there caused various new issues to appear.

After a period of groaning I decided that trying to make the codebase work on newer versions of mypy was worthwhile and something I had thought of doing previously anyway. I've tested on versions of mypy from 0.990 - 1.8.0. This PR does not change the pinned version of mypy it just makes it easier to make that change in the future.

Most everything the newer versions of mypy found were valid, with the exception of the detecting the cython based extension sources as empty namespace packages forcing me the turn off namespace package support. However, this seems to have had no negative side effects so it seems like a valid workaround for now.

One change (currently prefixed by xxx in the commit message) in the rook module seems to have a legit logical error. But I wasn't clear if my fix is correct. So I may need some help verifying it before taking the PR out of draft.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
    • Touches a number of different components, so you tell me if you really really want a ticket.
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)
Show available Jenkins commands
  • jenkins retest this please
  • jenkins test classic perf
  • jenkins test crimson perf
  • jenkins test signed
  • jenkins test make check
  • jenkins test make check arm64
  • jenkins test submodules
  • jenkins test dashboard
  • jenkins test dashboard cephadm
  • jenkins test api
  • jenkins test docs
  • jenkins render docs
  • jenkins test ceph-volume all
  • jenkins test ceph-volume tox
  • jenkins test windows
  • jenkins test rook e2e

@phlogistonjohn

Copy link
Copy Markdown
Contributor Author

@rkachach can you please take a look into the final commit in this PR (xxx: NOT VERIFIED: pybing/mgr/rook: change logic to prevent impossibl…). Because while I think I corrected an error I don't really know how to verify it. Let me know if it's OK or if we have a way to test it. I'm keeping the PR in draft until I hear from someone who is familiar enough with the rook module to give the patch a thumbs -up. Thanks!

Comment thread src/pybind/mgr/rook/rook_cluster.py Outdated

@tchaikov tchaikov Jan 22, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i am far from an expert in the rook mgr module, but i think the fix is correct. and probably a better fix would be to initialize sizematcher with AllMatcher('', None) instead, like

sizematcher: Matcher = AllMatcher('', None)
# ...
all 
or (device.sys_api['node'] in matching_hosts
    and sizematcher.compare(device)
    and ...
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, that suggestion is much clearer to read. I have pushed a change for that. I'll still wait for a general thumbs-up/down from someone with rook module experience, at least for a day or two.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@phlogistonjohn I think both your change and the improvement suggested by @tchaikov are correct. Anyway, as far as I know this code is not being used in rook latest versions anymore (it's part of OSDs creation which is now managed by the Rook orch).

@rkachach rkachach left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

rook related changes LGTM

Configure mypy not to use namespace packages as the cython source dirs
in src/pybind/{cephfs,rados,rbd}, etc. confuse mypy into thinking
it's an empty namespace package rather than a missing module.
Found using mypy 0.990.

Signed-off-by: John Mulligan <[email protected]>
When run locally mypy complains about the `name` field, possibly due to
us using an old mypy version on newer versions of python (we're stuck on
a Sept. 2022 version if you weren't aware). However, this could still be
an issue in mypy as there are open issues related to the `.name` field
of an enum. Regardless, we wouldn't know - so just work around it.

Signed-off-by: John Mulligan <[email protected]>
Found using mypy 0.990. Newer versions of mypy see that the callback
function passed to `addFilter` are expected to return a bool.  Update
`cherrypy_filter` to do have the correct type.

I did not try to fix the issue that this same function seems copy-pasted
into three different files. >:-}

Signed-off-by: John Mulligan <[email protected]>
Found using mypy 0.990.
Newer mypy versions see the check as tautological because `self.func` is
typed as always being `Callable` and thus always true. I don't know if
the typing is wrong or if the if-block is tautological (and redundant).
For now, silence the error by using `getattr` instead of direct
attribute access.

Signed-off-by: John Mulligan <[email protected]>
Found using mypy 0.990.
Fix mypy error by excluding both imports in the try-except block.
Type-narrow the `ex.request` variable with an assert.

Signed-off-by: John Mulligan <[email protected]>
Found using mypy 0.990. The `yaml_representer` function returns a Node
type, not Any. Newer versions of mypy understand this and complain.

Signed-off-by: John Mulligan <[email protected]>
Namespace package support confuses mypy in regards to the
pybind/{cephfs,rados,rbd}, etc when using mypy >= 0.990.
Turning them off seems to not be a problem for the ceph code.

Signed-off-by: John Mulligan <[email protected]>
@tchaikov

Copy link
Copy Markdown
Contributor

hi John, thank you for fixing the mypy issues! mypy did help us to address quite a few issues lurking around in the mgr modules when we introduced the type annotation system to the source tree. i am very glad it is still very helpful with your efforts.

This PR does not change the pinned version of mypy it just makes it easier to make that change in the future.

so, IIUC, there are still some issues identified by newer version of mypy even after your change, right?

@phlogistonjohn phlogistonjohn changed the title [WIP] Various mypy related updates Various mypy related updates Jan 23, 2024
@phlogistonjohn

Copy link
Copy Markdown
Contributor Author

All required checks are passing... moving to ready for review.

@phlogistonjohn
phlogistonjohn marked this pull request as ready for review January 23, 2024 19:26
@phlogistonjohn
phlogistonjohn requested review from a team as code owners January 23, 2024 19:26
@phlogistonjohn
phlogistonjohn requested review from aaSharma14 and avanthakkar and removed request for a team January 23, 2024 19:26
@phlogistonjohn

Copy link
Copy Markdown
Contributor Author

so, IIUC, there are still some issues identified by newer version of mypy even after your change, right?

Unless I overlooked something, no - the codebase should be clean for all currently released versions of mypy. However, not all versions of mypy work with all the python versions ceph currently supports. Plus, the way the tox.ini files are written in the ceph tree makes it difficult to try different combinations of mypy / python. On top of that I wanted to research if we could use the python version option. And I was hesitant to change it before the work getting centos 9 working was done. So I chose to wait and make the version bump in a later pr.

Comment thread qa/workunits/windows/test_rbd_wnbd.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd apply the same treatment to fio_size_mb on line 606 for consistency. cc @petrutlucian94

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. I wasn't sure if you preferred to have just the default value changed or changed + type annotated so I did the latter. I can undo it if that's an issue.

@adk3798 adk3798 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

cephadm/orchestrator pieces LGTM

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

just curious, did mypy actually complain about the use of Any here, or did you just want to make the type more specific

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It complained. But after looking into it again, it's not merely a new version of mypy alone but a newer version of types-PyYAML is also needed to get the errors:

orchestrator/__init__.py:6: note: In module imported here:
orchestrator/_interface.py:1284: error: Argument 2 to "add_representer" has incompatible type "Callable[[SafeDumper, DaemonDescription], Any]"; expected "Callable[[Dumper, DaemonDescription], Node]"  [arg-type]
orchestrator/_interface.py:1417: error: Argument 2 to "add_representer" has incompatible type "Callable[[SafeDumper, ServiceDescription], Any]"; expected "Callable[[Dumper, ServiceDescription], Node]"  [arg-type]
orchestrator/__init__.py:3: note: In module imported here:
orchestrator/module.py:136: error: Argument 2 to "add_representer" has incompatible type "Callable[[SafeDumper, HostDetails], Any]"; expected "Callable[[Dumper, HostDetails], Node]"  [arg-type]

Found using mypy 1.1.1.

Signed-off-by: John Mulligan <[email protected]>
Typing issue was found by mypy 1.5.1. The type only accepted values
of str but based on the data and code using set_column data the
boolean assignments to the 'isHidden' key were correct and so
I updated the type to also accept bool.

A more correct fix might be to use typing.TypedDict - this would
require python 3.8 (or typing extensions) and someone more familiar
with the dashboard code :-)

Signed-off-by: John Mulligan <[email protected]>
Issue found by mypy 1.6.1. The previous code was:
`and ((sizematcher != None) or sizematcher.compare(device)`
meaning that if sizematcher is not none "return" true, but if
sizematcher is not none execute the compare method. This is of course
impossible as None will never have a compare method. I assume that the

At Kefu Chai's suggestion we can replace the None condition with an
AllMatcher object when no size is provided to create a size matcher.
This both corrects the error and makes the code much simpler and more
readable.

Signed-off-by: John Mulligan <[email protected]>
Every time I looked at these functions my brain would be thrown off by
`all or ...` as I see all as the builtin function `all`:
    https://docs.python.org/3/library/functions.html#all

Rename `all` so that it avoids shadowing the function `all` and avoid
some confusion for people like me.

Signed-off-by: John Mulligan <[email protected]>

@Pegonzal Pegonzal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the mgr/dashboard changes look good as well

@phlogistonjohn

Copy link
Copy Markdown
Contributor Author

@adk3798 I think we have sufficient reviews from the various functional areas to look towards merging. Let me know if there's anything else that needs to be handled before then.

@adk3798
adk3798 merged commit 41652f8 into ceph:main Jan 29, 2024
@phlogistonjohn
phlogistonjohn deleted the jjm-mypy-updates branch January 30, 2024 14:39
@phlogistonjohn phlogistonjohn mentioned this pull request Mar 18, 2024
4 of 17 tasks
phlogistonjohn added a commit to phlogistonjohn/ceph that referenced this pull request Mar 19, 2024
In PR ceph#55255 I updated various files to be valid on newer versions of
mypy. Update the constrains file to specify mypy 1.1.1. This certainly
is not the newest version of mypy, but because ceph uses various older
versions of python I am being cautious and trying to only incrementally
update the mypy version. This version fixed an issue or two I hit while
working on a prototype manager module compared to the previous version
in the mypy-constrains.txt file.

Signed-off-by: John Mulligan <[email protected]>
adk3798 pushed a commit to adk3798/ceph that referenced this pull request Mar 27, 2024
In PR ceph#55255 I updated various files to be valid on newer versions of
mypy. Update the constrains file to specify mypy 1.1.1. This certainly
is not the newest version of mypy, but because ceph uses various older
versions of python I am being cautious and trying to only incrementally
update the mypy version. This version fixed an issue or two I hit while
working on a prototype manager module compared to the previous version
in the mypy-constrains.txt file.

Signed-off-by: John Mulligan <[email protected]>
(cherry picked from commit 6873a5c)
awojno-bloomberg pushed a commit to awojno-bloomberg/ceph that referenced this pull request Apr 23, 2024
In PR ceph#55255 I updated various files to be valid on newer versions of
mypy. Update the constrains file to specify mypy 1.1.1. This certainly
is not the newest version of mypy, but because ceph uses various older
versions of python I am being cautious and trying to only incrementally
update the mypy version. This version fixed an issue or two I hit while
working on a prototype manager module compared to the previous version
in the mypy-constrains.txt file.

Signed-off-by: John Mulligan <[email protected]>
afreen23 pushed a commit to rhcs-dashboard/ceph that referenced this pull request Jul 29, 2026
In PR ceph#55255 I updated various files to be valid on newer versions of
mypy. Update the constrains file to specify mypy 1.1.1. This certainly
is not the newest version of mypy, but because ceph uses various older
versions of python I am being cautious and trying to only incrementally
update the mypy version. This version fixed an issue or two I hit while
working on a prototype manager module compared to the previous version
in the mypy-constrains.txt file.

Signed-off-by: John Mulligan <[email protected]>
(cherry picked from commit b1fc58e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

6 participants