Skip to content

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

Merged
merged 1 commit into from
Dec 6, 2017

Conversation

jschendel
Copy link
Member

  • passes 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.

@jschendel jschendel force-pushed the cython-property-syntax branch from 05a7d26 to 501e9ce Compare December 2, 2017 21:40
@codecov
Copy link

codecov bot commented Dec 2, 2017

Codecov Report

Merging #18602 into master will decrease coverage by 0.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            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
Flag Coverage Δ
#multiple 89.32% <ø> (ø) ⬆️
#single 40.6% <ø> (-0.11%) ⬇️
Impacted Files Coverage Δ
pandas/io/gbq.py 25% <0%> (-58.34%) ⬇️
pandas/core/frame.py 97.81% <0%> (-0.1%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0e16818...501e9ce. Read the comment docs.

@codecov
Copy link

codecov bot commented Dec 2, 2017

Codecov Report

Merging #18602 into master will decrease coverage by 0.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            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
Flag Coverage Δ
#multiple 89.44% <ø> (ø) ⬆️
#single 40.67% <ø> (-0.11%) ⬇️
Impacted Files Coverage Δ
pandas/io/gbq.py 25% <0%> (-58.34%) ⬇️
pandas/core/frame.py 97.81% <0%> (-0.1%) ⬇️
pandas/util/testing.py 82.01% <0%> (+0.19%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a764663...6b136cf. Read the comment docs.

def value(self):
return self.value

@value.setter
Copy link
Contributor

Choose a reason for hiding this comment

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

don’t add this

Copy link
Member Author

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.

@jschendel jschendel force-pushed the cython-property-syntax branch from 501e9ce to 5a9f664 Compare December 2, 2017 22:23
return self.value
@property
def value(self):
return self.value
Copy link
Member

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?

Copy link
Member Author

@jschendel jschendel Dec 3, 2017

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.

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.

Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Member

@gfyoung gfyoung Dec 3, 2017

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.

Copy link
Contributor

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

Copy link
Member Author

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.

Copy link
Contributor

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

Copy link
Member Author

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!

@gfyoung gfyoung added the Code Style Code style, linting, code_checks label Dec 3, 2017
@jreback
Copy link
Contributor

jreback commented Dec 3, 2017

so the .values value is actually set only via a c call, IOW this is not a python settable propery

@jbrockmendel
Copy link
Member

so the .values value is actually set only via a c call, IOW this is not a python settable propery

This 100% makes sense. Regardless, the property is almost certainly not needed.

@jreback jreback added the Build Library building on various platforms label Dec 3, 2017
@jschendel
Copy link
Member Author

Would it be worthwhile to add a check for legacy syntax to lint.sh? Or is this something that's infrequent enough to not bother with?

@jreback
Copy link
Contributor

jreback commented Dec 3, 2017

i don’t think necessary to add a lint check here

@jschendel jschendel force-pushed the cython-property-syntax branch from 5a9f664 to 6b136cf Compare December 4, 2017 20:21
@jschendel
Copy link
Member Author

Green after rebase to include the Cython version bump. Didn't need to add a setter or any additional code.

@jreback jreback added this to the 0.22.0 milestone Dec 6, 2017
@jreback jreback merged commit 19ce05e into pandas-dev:master Dec 6, 2017
@jreback
Copy link
Contributor

jreback commented Dec 6, 2017

thanks!

@jschendel jschendel deleted the cython-property-syntax branch December 6, 2017 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Build Library building on various platforms Code Style Code style, linting, code_checks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants