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

Ruff settings #927

Open
antirotor opened this issue Oct 1, 2024 · 8 comments
Open

Ruff settings #927

antirotor opened this issue Oct 1, 2024 · 8 comments
Assignees
Labels
type: enhancement Improvement of existing functionality or minor addition

Comments

@antirotor
Copy link
Member

antirotor commented Oct 1, 2024

Problem

Current ruff settings are to prohibitive.

We can reverse the logic, select all rules and just ignore those that we must. Here is the list of rules I had to disable in 3dequalizer addon for some reasons I'll try to explain here:

  • D203 - one-blank-line-before-class: exclusive with D2111
  • D213 - multi-line-summary-second-line, exclusive with D2121
  • D406 - new-line-after-section-name 1
  • D407 - dashed-underline-after-section 1
  • PTH - this is ignoring all "use pathlib" warnings. We have a lot of os.path logic all over the place and rewriting it all to make use of pathlib would be major work.
  • ANN101 - this is deprecated check where self should be type annotated for the sake of older IDE that couldn't infer the type.
  • ANN102 - the same thing as ⬆️ but for cls
  • ANN204 - return type annotation for special methods. This requires even __init__() to have that2
  • COM812 - missing-trailing-comma - while this rule make sense, it is enforcing trailing commas even within function arguments if multiline.
  • S603 - subprocess-without-shell-equals-true. Requires validation of input arguments, but it is vague how to achieve it?
  • ERA001 - commented-out-code - code that is commented out is IMO still valuable as illustration or example.
  • TRY003 - raise-vanilla-args - define new exception if message is too long. This just adds more code that needs to be maintained.
  • UP007 - non-pep604-annotation - since we need to support python 3.7, we can't use | in type annotations.
  • ARG002 - unused-method-argument - for method that are overriden, even unused arguments should be listed for clarity, no?
  • INP001 - implicit-namespace-package - this rule is forcing __init__.py to be almost in all directories with any python files even in a case where nothing will ever be imported from there.
    for docstrings ("D" rules) we should add pydocstyle.convention = "google"
  • UP006 - non-pep585-annotation - this is for compatibility with py37 - List[int] = [1, 2, 3] vs. list[int] = [1, 2, 3]
  • UP007 - non-pep604-annotation - this is for compatibility with py37 - foo: Union[int, str] = 1 vs. foo: int | str = 1
  • UP035 - deprecated-import - py37 compatibility too, when you need to import something from deprecated location

Some notes:

  • Most of the error are due to the docstrings - like missing package level docstring, etc. D100, D104, D107
  • printf style formatting: there are some collisions with how we use printf style in logging. We shouldn't basically. We should make use extra argument in logging:
# wrong
self.log.info(f"this is {error}")

# also wrong
self.log.info("this is %s" % error)

# also wrong
self.log.info("this is {}".format(error))

# this is right
self.log.info("this is %s, error)

Footnotes

  1. This should be solved by pydocstyle.convention = "google" so no need to ignore 2 3 4

  2. Unless mypy-init-return is used in ruff config.

@antirotor antirotor added the type: enhancement Improvement of existing functionality or minor addition label Oct 1, 2024
@antirotor antirotor self-assigned this Oct 1, 2024
@antirotor
Copy link
Member Author

added INP001, UP035, UP006, UP007

@iLLiCiTiT
Copy link
Member

iLLiCiTiT commented Oct 11, 2024

printf style formatting: there are some collisions with how we use printf style in logging. We shouldn't basically. We should make use extra argument in logging:

Slows down pyblish plugins logging. Also when using dict/list/more complex objects, you have to explicitly convert it to string.

@BigRoy
Copy link
Collaborator

BigRoy commented Oct 11, 2024

Slows down pyblish plugins logging.

How does it slow it down? Because it should speed it up because it'd skip formatting logs that remain unhandled.

I mean, technically it could be slower because we just happen to capture ALL logs.

Also when using dict/list/more complex objects, you have to explicitly convert it to string.

Regarding this - I believe that's actually an issue of how the 'logs' are handled by us - we store handle the unformatted string I suppose instead of the formatted one? Which means we format it later when the object has changed in the meantime? I've never seen this occur with regular logging at least - but may be just me.

@iLLiCiTiT
Copy link
Member

iLLiCiTiT commented Oct 11, 2024

I mean, technically it could be slower because we just happen to capture ALL logs.

We do, and pyblish does it too, and if it's logged to console, then it is formatted 3 times. Any additional handler is +1 formatting.

Regarding this - I believe that's actually an issue of how the 'logs' are handled by us - we store handle the unformatted string I suppose instead of the formatted one? Which means we format it later when the object has changed in the meantime? I've never seen this occur with regular logging at least - but may be just me.

It does not happen in pyblish because we're handling it at 2 places. We do format the message at the time of emiting instead of using, so we get what was logged at the time of emit.

@iLLiCiTiT
Copy link
Member

Anyways, good start. We can discuss more, when we start to apply them to repositories.

@antirotor
Copy link
Member Author

How does it slow it down? Because it should speed it up because it'd skip formatting logs that remain unhandled.

printf style is slowing stuff down because it is always evaluated even in the case the log isn't emitted after all. But that is the case for all formatting except the last mentioned, the extra log() argument.

@iLLiCiTiT
Copy link
Member

iLLiCiTiT commented Oct 11, 2024

printf style is slowing stuff down because it is always evaluated even in the case the log isn't emitted after all.

In pyblish plugins are all logs emited, and are always formatted at the time of their emit. At least log handlers that are used by default (and it is not possible to avoid using them without rewriting pyblish). So, for pyblish logging it is actually faster to use f-string, because it is formatted only once. If we would use log formatting then it is formatted 1x + len(handlers) 100% (at least 2x in publisher tool).

If we would not do it then we would get invalid logs:

data = {}
self.log.info("%s", data)
data["key"] = "value"
self.log.info("%s", data)
>>> INFO: {'key': 'value'}
>>> INFO: {'key': 'value'}

@antirotor
Copy link
Member Author

antirotor commented Oct 11, 2024

In that case we can add exception for pyblish plugins. Something like this could work:

[tool.ruff.lint.per-file-ignores]
"*/plugins/publish/*.py" = ["G002"]  # because of how logging is done in pyblish

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement Improvement of existing functionality or minor addition
Projects
None yet
Development

No branches or pull requests

3 participants