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

Enable many ruff checks #476

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

Conversation

inknos
Copy link
Contributor

@inknos inknos commented Nov 29, 2024

Depends on #473

Copy link
Contributor

openshift-ci bot commented Nov 29, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: inknos

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@inknos
Copy link
Contributor Author

inknos commented Dec 2, 2024

podman/domain/images_manager.py:291: in ImagesManager
    body: list[dict[str, Any]],  # noqa: A003
E   TypeError: 'function' object is not subscriptable

The errors come from removing the deprecated typing imports for Dict, List, Tuple and Type. Specifically, list is used in place of List and it's shadowed by the list method. Need to think of how to go around the issue without breaking the api in the first place

@inknos inknos force-pushed the enable-many-ruff-checks branch 8 times, most recently from 88768a6 to 15d23f3 Compare December 3, 2024 15:07
@inknos inknos force-pushed the enable-many-ruff-checks branch 2 times, most recently from 8533cf6 to ea3a31c Compare December 10, 2024 15:25
@inknos inknos force-pushed the enable-many-ruff-checks branch 3 times, most recently from 80ee029 to 34b2d49 Compare December 10, 2024 15:42
@inknos inknos marked this pull request as ready for review December 10, 2024 16:07
@inknos inknos force-pushed the enable-many-ruff-checks branch from 34b2d49 to 3be44da Compare December 11, 2024 16:50
@@ -31,6 +32,8 @@

logger = logging.getLogger("podman.images")

builtin_list = list

Copy link
Member

Choose a reason for hiding this comment

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

From my reading:

import builtins
>>> builtins.list

is more explicit about what is happening in this code. We are stuck with the naming collision. I think the safety of the typing is more important than the code bloat this will introduce. Or we can stay with List[].

Copy link
Contributor Author

Choose a reason for hiding this comment

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

typing.List is deprecated, that's why we should move away from it if we can.

https://docs.python.org/3/library/typing.html#typing.List

I like your solution better though, it's more clear. I believe the list shadowing will not be problematic for users though, but the ambiguity in returns would eventually flag unexpected errors in IDEs, testing, checkers and such.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jwhonce I updated the PR with your suggestion

Copy link
Member

Choose a reason for hiding this comment

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

@inknos The joys of trying to graph type checking on a language that is not statically typed.

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 know...

@inknos inknos force-pushed the enable-many-ruff-checks branch from 3be44da to 7f1eaee Compare December 12, 2024 15:26
Copy link
Member

@jwhonce jwhonce left a comment

Choose a reason for hiding this comment

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

Some nits and some questions. Looking good!

podman/domain/images_manager.py Outdated Show resolved Hide resolved
podman/domain/images_manager.py Outdated Show resolved Hide resolved
podman/domain/images_manager.py Outdated Show resolved Hide resolved
podman/domain/manager.py Outdated Show resolved Hide resolved
podman/domain/networks_manager.py Outdated Show resolved Hide resolved
Catching BaseException can make it hard to interrupt the program
(e.g., with Ctrl-C) and can disguise other problems.

Signed-off-by: Nicola Sella <[email protected]>
Checks for the use of the characters 'l', 'O', or 'I' as variable names.

Signed-off-by: Nicola Sella <[email protected]>
F401: unused-import

E402: module-import-not-at-top-of-file

S101: assert

Assertions are removed when Python is run with optimization requested
(i.e., when the -O flag is present), which is a common practice in
 production environments. As such, assertions should not be used for
runtime validation of user input or to enforce interface constraints.

Signed-off-by: Nicola Sella <[email protected]>

Unused import

Signed-off-by: Nicola Sella <[email protected]>
Check for unused variables. Unused variables should be prefixed with '_'

Signed-off-by: Nicola Sella <[email protected]>
Signed-off-by: Nicola Sella <[email protected]>
This is a quality of life improvement and it should be backward
compatible with our previous set line-length

Signed-off-by: Nicola Sella <[email protected]>
Fix sys.version_info comparisons and drop unsupported python code

Furthermore, addresses UP008: super-call-with-parameters

Super is not called with parameters anymore when the first argument is
__class__ and the second argument is equivalent to the first argument of
the enclosing method

Signed-off-by: Nicola Sella <[email protected]>
It is recommended to use 2 or more to give the caller more context about
warning

Signed-off-by: Nicola Sella <[email protected]>
B024: abstract-base-class-without-abstract-method
Usually, abstract classes with no abstract methods are flagged
as incorrectly implemented because @abstract might have been
forgotten.

PodmanResource should be kept an abstract class to prevent
the initialization of an instance of it

Signed-off-by: Nicola Sella <[email protected]>
Bugbear checks usually check for design problems within the code.

https://pypi.org/project/flake8-bugbear/

Signed-off-by: Nicola Sella <[email protected]>
This could be an exception and should be checked in the future but it is
suppressed at the moment.

Signed-off-by: Nicola Sella <[email protected]>
S603: subprocess-without-shell-equals-true

This could be an exception or a false positive and since it's used on
one single piece of code it is ok to ignore from now.

Signed-off-by: Nicola Sella <[email protected]>
Bandit provides security checks and good practices suggestions for the
codebase.

https://pypi.org/project/flake8-bandit/

Signed-off-by: Nicola Sella <[email protected]>
Signed-off-by: Nicola Sella <[email protected]>
Fix errors for tyope annotation for list, dict, type and tuple

Example:
UP006: Use `list` instead of `List` for type annotation

Signed-off-by: Nicola Sella <[email protected]>
Python Version is already set to be < 3.9 so this code will never run

Signed-off-by: Nicola Sella <[email protected]>
This suppresses A003 for `list` builtin.

`typing.List`, `typing.Dict`, `typing.Tuple` and `typing.Type` are
deprecated. Removing these annotations breaks the calls to `list` when
they are done within the same class scope, which makes them ambiguous.

Typed returns `list` resolve to the function `list` defined in the class,
shadowing the builtin function. This change is not great but a proper
one would require changing the name of the class function `list` and
breaking the API to be fixed.

Example of where it breaks:

podman/domains/images_manager.py

class ImagesManager(...):

    def list(...):
        ...

    def pull(
        self,
        ...
        ) -> Image | list[Image], [[str]]:
        ...

Here, the typed annotation of `pull` would resolve to the `list` method,
rather than the builtin.

For the sake of readability, all builtin `list` calls are replaced in
the class as `builtins.list`.

Signed-off-by: Nicola Sella <[email protected]>
Signed-off-by: Nicola Sella <[email protected]>
@inknos inknos force-pushed the enable-many-ruff-checks branch from 7f1eaee to 995f760 Compare December 13, 2024 13:10
@inknos
Copy link
Contributor Author

inknos commented Dec 13, 2024

@jwhonce I addressed the builtins.list comments. now the calls are explicit within the classes with the shadowed list methods. Then, I removed api.Literal and brought back ABC abstract subclassing.

Also, I removed the last commit as it was more like a placeholder for future ruff checks and I don't like it upstream but rather track it in a different way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants