-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
STYLE: Use decorator syntax instead of legacy syntax for defining properties in Cython #18602
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
Conversation
05a7d26
to
501e9ce
Compare
Codecov Report
@@ Coverage Diff @@
## master #18602 +/- ##
==========================================
- Coverage 91.46% 91.45% -0.02%
==========================================
Files 157 157
Lines 51439 51439
==========================================
- Hits 47051 47042 -9
- Misses 4388 4397 +9
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #18602 +/- ##
==========================================
- Coverage 91.59% 91.58% -0.02%
==========================================
Files 155 155
Lines 51255 51255
==========================================
- Hits 46948 46941 -7
- Misses 4307 4314 +7
Continue to review full report at Codecov.
|
pandas/_libs/tslibs/conversion.pyx
Outdated
def value(self): | ||
return self.value | ||
|
||
@value.setter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don’t add this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, some 2.7 builds failed earlier without it, and it looked like there were places where value
was being set, so I added that to see if it would resolve the issue since I couldn't reproduce locally. Looks like that wasn't the issue.
501e9ce
to
5a9f664
Compare
return self.value | ||
@property | ||
def value(self): | ||
return self.value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been kinda curious how this works namespace-wise. Why isn't this a recursion error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, now that you mention it, this does seem a bit strange, even the way it was originally written with the legacy syntax. I tried creating similar classes in plain Python and they all raised.
That aside, looking further into the code, it doesn't seem like value
be a property? Or should at least have a setter? Maybe I'm missing something obvious (not super familiar with this part of the codebase or Cython in general), but it looks like there are places where value
is being set, e.g.
pandas/pandas/_libs/tslibs/conversion.pyx
Lines 250 to 256 in 0e16818
obj = _TSObject() | |
if is_string_object(ts): | |
return convert_str_to_tsobject(ts, tz, unit, dayfirst, yearfirst) | |
if ts is None or ts is NaT: | |
obj.value = NPY_NAT |
(and many other examples further below that)
Trying something similar on master
raises, albeit in plain Python:
In [1]: from pandas._libs.tslibs.conversion import _TSObject
In [2]: obj = _TSObject()
In [3]: obj.value = 10
---------------------------------------------------------------------------
AttributeError: attribute 'value' of 'pandas._libs.tslibs.conversion._TSObject' objects is not writable
So I'm a little confused as to how this working in the snippet I posted from the codebase, but again, could just be my relative inexperience with Cython speaking.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are the test failures related to the change in this discussion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gfyoung : I believe so, but I'm not 100% sure.
The test failure reads:
File "pandas/_libs/tslibs/conversion.pyx", line 268, in pandas._libs.tslibs.conversion.convert_to_tsobject (pandas/_libs/tslibs/conversion.c:3771)
obj.value = ts
AttributeError: can't set attribute
which is just a few lines below the snipped from conversion.pyx
I posted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, maybe it needs a setter as well. I though this was a problem as was trying to access a c-attribute from a non-cdef, but that's not the case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't you revert your changes to this property
syntax so that you have a clean commit that passes all tests. Then you can mess around with this problematic instance in a separate commit if need be.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what version of cython supports this?
not averse to bumping it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like 0.24.0. Was able to reproduce the error locally using 0.23.x, then get it working on 0.24.0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok would take this as an update then to 0.24
need to update setup.py, some ci files, install.rst and add a more in whatsnew
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. Will open an issue to have something to reference in the whatsnew, and give this more visibility on the off chance that someone has an objection to the bump. And here I was thinking that this PR would be an easy and straightforward way to start getting into to the Cython part of the codebase!
so the |
This 100% makes sense. Regardless, the property is almost certainly not needed. |
Would it be worthwhile to add a check for legacy syntax to |
i don’t think necessary to add a lint check here |
5a9f664
to
6b136cf
Compare
Green after rebase to include the Cython version bump. Didn't need to add a setter or any additional code. |
thanks! |
git diff upstream/master -u -- "*.py" | flake8 --diff
See the Cython docs for a comparison of the decorator syntax and legacy syntax. I only found a few instances of the legacy syntax; looks like most of the Cython code is already using decorator syntax.