polars icon indicating copy to clipboard operation
polars copied to clipboard

feat(python): expose cat.ordered

Open ritchie46 opened this issue 2 years ago • 9 comments

#5671

ritchie46 avatar Dec 02 '22 13:12 ritchie46

Somehow the docs fail because the self._s is not initialized in the CatNameSpace. This is probably because of our complicated dispatch.

Any idea here @alexander-beedie?

ritchie46 avatar Dec 02 '22 13:12 ritchie46

Any idea here @alexander-beedie?

Am currently in the middle of the Jordanian desert! 😂 Will be back in range of a computer on Monday evening if you can wait a bit - I'm sure it'll be something simple to resolve (quick sanity check: the CatNameSpace has the same decorator as the others?)

alexander-beedie avatar Dec 03 '22 10:12 alexander-beedie

ning if you can wait a bit - I'm sure it'll be something simple to resolve (quick sanity check: the CatNameSpace has the same decorator as the others?)

No hurry at all! Have fun there in the dessert. ;)

ritchie46 avatar Dec 03 '22 17:12 ritchie46

Any idea here @alexander-beedie?

Refreshed; back home from... Mars?! (Feel like I should watch Dune again - part of it was shot out there ;)

Wadi Rum Desert

Ok, so should be able to fix like so:

return self._s.cat_is_ordered() if self._s is not None else None

The reason is that the *NameSpace classes are accessed with an uninitialised Series from Sphinx (via the @accessor decorator in series.py which makes the namespaces available under the correct alias when docs are built). So, when the property is introspected by Sphinx (and only then) _s is still None, hence the error.

Could equally check the docs-build env var and return None if it is set:

if os.getenv("BUILDING_SPHINX_DOCS"):
    return None
return self._s.cat_is_ordered()

alexander-beedie avatar Dec 06 '22 10:12 alexander-beedie

Wow, that's some sight indeed. How did my @alexander-beedie tag even reach you there? :see_no_evil:

ritchie46 avatar Dec 07 '22 07:12 ritchie46

Wow, that's some sight indeed. How did my @alexander-beedie tag even reach you there? 🙈

Heh... there was one spot in the camp with limited WiFi, so could check-up on the outside world at dinner/breakfast, lol.

I see the proposed fix didn't quite work; not sure why it's claiming to want a method signature out of a None value property, but can dig further tonight.

alexander-beedie avatar Dec 07 '22 08:12 alexander-beedie

Hmm... I setup a debug profile for sphinx-build so I could step through and see what's happening; have tried several increasingly-exotic attempts to keep it happy, but the least-intrusive solution so far is simply to omit the @property decorator and leave it as a method - at which point everything appears to flow through smoothly (and you can drop the special None-handling code inside the method body):

def ordered(self) -> bool:
    """Return if sorting uses the categories or the lexical order of the string values."""  # noqa: E501
    return self._s.cat_is_ordered()

(Update: just had one last idea to potentially maintain as a property - will see if it's viable tomorrow morning).

alexander-beedie avatar Dec 07 '22 20:12 alexander-beedie

The final idea "almost" worked - was playing with a @namespace_property decorator (a slight variation on @accessor) and that allowed the docs to compile fine without any other special handling, but with one irritating issue - the generated docs reported the ordered attribute as a method. It seems the universe really wants this to be a method...😅

alexander-beedie avatar Dec 08 '22 12:12 alexander-beedie

I take it back - have managed to make a patch on top of this one that allows us to better-handle namespaced attributes, if you want to grab it; can take a look here: https://github.com/pola-rs/polars/commit/ebebd67e94d604b4e5f1eb4224ecb0608077ec9f. Allows us to use @property as normal, with no special handling, without raising sphinx-build errors.

The docs formatting as a method (above) came down to use of the wrong .rst accessor template, so that was an easy fix after all. The one thing that's still a bit annoying is that the description/docstring and expected type aren't being pulled into the generated docs; instead it's reporting the default attribute value (False). I can revisit this later to see if the template needs updating, or if there is something else at play, but at least it's in better shape now.

So, now have a reasonable choice between making it a method (everything works), or leaving as an attribute (everything works except for getting the complete description/docstring in the docs output).

alexander-beedie avatar Dec 08 '22 18:12 alexander-beedie

@ritchie46: let's just make ordered a method - path of least resistance, and everything will "just work". (I can potentially revisit after upgrading to sphinx v6).

alexander-beedie avatar Feb 07 '23 10:02 alexander-beedie

Closing in favor of #10325 as I didn't feel like rebasing 😄

stinodego avatar Aug 06 '23 19:08 stinodego