Skip to content

chore: Adopting more ruff complexity rules#2460

Merged
FBruzzesi merged 14 commits intomainfrom
ruff-complexity-rules
May 4, 2025
Merged

chore: Adopting more ruff complexity rules#2460
FBruzzesi merged 14 commits intomainfrom
ruff-complexity-rules

Conversation

@dangotbanned
Copy link
Copy Markdown
Member

Will close #2376

What type of PR is this? (check all applicable)

  • 💾 Refactor
  • ✨ Feature
  • 🐛 Bug Fix
  • 🔧 Optimization
  • 📝 Documentation
  • ✅ Test
  • 🐳 Other

Related issues

Checklist

  • Code follows style guide (ruff)
  • Tests added
  • Documented the changes

If you have comments or can explain your changes, please do so below

Important

Currently just highlighting areas that need some love

Would need some buy-in from others that they'd like to see this stuff simplified/broken up 🙂

@dangotbanned dangotbanned marked this pull request as ready for review May 1, 2025 17:35
@FBruzzesi
Copy link
Copy Markdown
Member

Thanks @dangotbanned - I am a bit perplexed about this one as of now, mostly due to

Currently just highlighting areas that need some love

This does not mean I am against it, but as of now I don't see the benefits nor what it would imply to adopt them

Would need some buy-in from others

Could you maybe show how one of those noqa would change to satisfy these rules?

According to such example it might be easier for everyone to evaluate how the future might look like 😁

@dangotbanned
Copy link
Copy Markdown
Member Author

#2460 (comment)

Sure thing @FBruzzesi I'll take a stab at one tomorrow!
The general theme would be writing shorter, more reusable and easier to understand functions.

The aim of the limits set by the rules are to nudge you when something you're writing is starting to get difficult for others to understand.
You don't necessarily need to follow them all the time, but I've found it keeps me on my toes - always considering "will this make sense to someone else, without writing a comment?"


@functools.lru_cache(maxsize=16)
def non_object_native_to_narwhals_dtype(native_dtype: Any, version: Version) -> DType:
def non_object_native_to_narwhals_dtype(native_dtype: Any, version: Version) -> DType: # noqa: C901, PLR0912
Copy link
Copy Markdown
Member Author

@dangotbanned dangotbanned May 3, 2025

Choose a reason for hiding this comment

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

This could benefit from something like the example in (PLR0911)

PANDAS_LIKE_TO_NW_PRIMITIVE

PANDAS_LIKE_TO_NW_PRIMITIVE = {
    "Int64": "Int64",
    "Int64[pyarrow]": "Int64",
    "int64": "Int64",
    "int64[pyarrow]": "Int64",
    "Int32": "Int32",
    "Int32[pyarrow]": "Int32",
    "int32": "Int32",
    "int32[pyarrow]": "Int32",
    "Int16": "Int16",
    "Int16[pyarrow]": "Int16",
    "int16": "Int16",
    "int16[pyarrow]": "Int16",
    "Int8": "Int8",
    "Int8[pyarrow]": "Int8",
    "int8": "Int8",
    "int8[pyarrow]": "Int8",
    "UInt64": "UInt64",
    "UInt64[pyarrow]": "UInt64",
    "uint64": "UInt64",
    "uint64[pyarrow]": "UInt64",
    "UInt32": "UInt32",
    "UInt32[pyarrow]": "UInt32",
    "uint32": "UInt32",
    "uint32[pyarrow]": "UInt32",
    "UInt16": "UInt16",
    "UInt16[pyarrow]": "UInt16",
    "uint16": "UInt16",
    "uint16[pyarrow]": "UInt16",
    "UInt8": "UInt8",
    "UInt8[pyarrow]": "UInt8",
    "uint8": "UInt8",
    "uint8[pyarrow]": "UInt8",
    "Float64": "Float64",
    "Float64[pyarrow]": "Float64",
    "double[pyarrow]": "Float64",
    "float64": "Float64",
    "float64[pyarrow]": "Float64",
    "Float32": "Float32",
    "Float32[pyarrow]": "Float32",
    "float32": "Float32",
    "float32[pyarrow]": "Float32",
    "float[pyarrow]": "Float32",
    "large_string[pyarrow]": "String",
    "string": "String",
    "string[pyarrow]": "String",
    "string[python]": "String",
    "bool": "Boolean",
    "bool[pyarrow]": "Boolean",
    "boolean": "Boolean",
    "boolean[pyarrow]": "Boolean",
}

We can then reduce the first 12 branches into a single case

@functools.lru_cache(maxsize=16)
def non_object_native_to_narwhals_dtype(native_dtype: Any, version: Version) -> DType:  # noqa: C901, PLR0912
    dtype = str(native_dtype)

    dtypes = version.dtypes
    if normalized := PANDAS_LIKE_TO_NW_PRIMITIVE.get(dtype):
        return getattr(dtypes, normalized)()
    elif ...

return self.with_native(ser.native.cast(pa.timestamp(self.unit, time_zone)))

def timestamp(self, time_unit: TimeUnit) -> ArrowSeries:
def timestamp(self, time_unit: TimeUnit) -> ArrowSeries: # noqa: C901, PLR0912
Copy link
Copy Markdown
Member Author

@dangotbanned dangotbanned May 3, 2025

Choose a reason for hiding this comment

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

Note

TODO Done

Okay I'll do this one in this PR - have been wanting to refactor it for a while anyway

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@FBruzzesi first worked example in (6666fe2)

I could have reduced the LOC as well, but I think the typing helps reduce the amount of info you need to keep in your head 🙂

Copy link
Copy Markdown
Member

@FBruzzesi FBruzzesi May 4, 2025

Choose a reason for hiding this comment

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

Yep that's great (as commented here) 🙌🏼 rethinking functorialities in this way is pretty helpful for long term maintenance!

I would propose to:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks @FBruzzesi - I've finished moving all of them into child issues of

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am already sold from this diff 👌🏼😉

@dangotbanned dangotbanned changed the title chore(RFC): Adopting more ruff complexity rules chore: Adopting more ruff complexity rules May 4, 2025
@dangotbanned dangotbanned requested a review from FBruzzesi May 4, 2025 18:52
Copy link
Copy Markdown
Member

@FBruzzesi FBruzzesi left a comment

Choose a reason for hiding this comment

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

Thanks @dangotbanned 👌🏼 Happy to ship 🚢 so that we can start to address all methods

GiveMeFiveGoodJobGIF

@FBruzzesi FBruzzesi merged commit 7ea5044 into main May 4, 2025
30 of 31 checks passed
@FBruzzesi FBruzzesi deleted the ruff-complexity-rules branch May 4, 2025 21:06
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.

[internal]: Adopting/following more ruff complexity rules

2 participants