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

Make Column or MaskedColumn name arg be a keyword arg name=None #731

Merged
merged 7 commits into from Feb 8, 2013

Conversation

taldcroft
Copy link
Member

The first arg for the Column or MaskedColumn is name. In this PR name is changed to a keyword argument name=None. This makes name optional, but more importantly would allow the documentation to recommend creating a column with:

c = Column(data=[1,2], name='a')

This would get people writing code with data and name both provided as kwargs, which would smooth the transition to 0.3 --> we would also include in the documentation a heads-up that the plan is to change the keyword argument ordering in 0.3 to Column(data=None, name=None, ...), so that from 0.3 you could write:

c= Column([1,2], name='a')

As mentioned in #726 this ordering is consistent with Table and recarray. This PR is in lieu of actually changing the order now. That would be possible but is a significant API change (which in theory is still open for discussion @iguananaut) affecting many files and would likely delay release of 0.2

If this PR instead is accepted for 0.2 then the documentation update and additional tests will be provided.

@astrofrog
Copy link
Member

This looks good to me. Do you want to add the docs and tests here, or in a separate pull request?

@taldcroft
Copy link
Member Author

I'll add docs and tests here.

@mdboom
Copy link
Contributor

mdboom commented Feb 5, 2013

👍 on improving the consistency with Numpy -- this tripped me up the first time I saw it, too.

@eteq
Copy link
Member

eteq commented Feb 5, 2013

👍

@taldcroft
Copy link
Member Author

As far as I'm concerned, this is done. Tests pass locally. You can look at the new red warning here:

http://hea-www.harvard.edu/~aldcroft/tmp/astropy/html/table/construct_table.html#column-and-tablecolumns-classes

@iguananaut - this should be backported to 0.2 if possible since it provides advance notice of pending API change in 0.3.

@astrofrog
Copy link
Member

This looks good to me! Maybe someone else should just double check too, but then it's good to merge.

@eteq
Copy link
Member

eteq commented Feb 7, 2013

Looks fine to me, as well.

This is implemented as a decorate on __new__(cls, name=None, data=None, ..)
for Column and MaskedColumn.

The warning is:

In the next major release of astropy (0.3), the order of function
arguments for creating a {class_name} will change.  Currently the order is
{class_name}(name, data, ...), but in 0.3 and later it will be
{class_name}(data, name, ...).  This is consistent with Table and NumPy.

In order to use the same code for Astropy 0.2 and 0.3, column objects
should be created using named keyword arguments for data and name, e.g.:
{class_name}(name='a', data=[1, 2]).
This is only complete for table and io.ascii.  All of these tests
run without generating warnings.  This was done to be sure that
all code in astropy.table and astropy.io.ascii is fully compliant
with the new Column args checking, i.e. it will never happen that
a call to astropy.Table can generate a warning to the user.
@taldcroft
Copy link
Member Author

@astrofrog @eteq - based on discussion in #726, I made a decorator around Column.__new__ that checks for any "bare" args like Column('a', ..) and issues a warning. See 716dde9.

I ran all tests for table and io.ascii with stdout capturing disabled to confirm that all this code complies with the current standard of using only keyword args with Column for 0.3 compatibility. This was a slight pain as a lot of changes had to be made in 69ed301, but it means that most of the hard work for #726 is now done. There are still some tests within io.misc and coordinates which will generate warnings, but these are normally suppressed and not a problem. I'll fix those later. Based on my grepping through every *.py file in astropy there are no other calls to Column or MaskedColumn that would generate a warning.

This is sort of a bigger change than I was hoping for, but I think it's good to actually generate warnings instead of just docs. This way there should be absolutely no issue with the arg order change in 0.3.

def wrapper(*args, **kwargs):
if len(args) > 1:
cls = args[0] # Column or MaskedColumn class from __new__(cls, ..)
warnings.warn(WARN_COLUMN_ARGS_MESSAGE.format(class_name=cls.__name__))
Copy link
Member

Choose a reason for hiding this comment

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

This is not a major thing, but might be nice: perhaps you should use a specific Warning subclass here? Either a custom one you define just for this, or maybe just DeprecationWarning (this isn't exactly a deprecation, but it's similar). That makes it easier to put in the warnings filter if someone doesn't want the constant barrage of messages. (Although maybe that's what you want?)

Copy link
Member Author

Choose a reason for hiding this comment

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

The first version did exactly this and used FutureWarning. But this gave a slightly weird output so I went back to the default.

WARNING: FutureWarning: In the next major release of astropy (0.3), the order of function
arguments for creating a Column will change.  Currently the order is
Column(name, data, ...), but in 0.3 and later it will be
Column(data, name, ...).  This is consistent with Table and NumPy.
...

There is no barrage, after the first warning you never see it again in that session (unlike Quantity float conversion for instance). I was imagining the user would set WARN_COLUMN_ARGS to False to disable this entirely. I purposely did not document this fact because nobody should ever do that, they just need to fix their code...

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see - I was thinking you meant to have this warn every time, rather than just once, and I didn't notice WARN_COLUMN_ARGS.

I don't see where WARN_COLUMN_ARGS is used, though. shouldn't there be an if WARN_COLUMN_ARGS(): above the warning call?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't see where WARN_COLUMN_ARGS is used, though. shouldn't there be an if WARN_COLUMN_ARGS(): above the warning call?

Doohh! Good catch. Fixed in f91436c.

Copy link
Member

Choose a reason for hiding this comment

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

Great, looks good now, then.

I wonder if we should be using ConfigurationItems when we know the setting will be temporary? (that is, presumably it will disappear in 0.3). No need to change this here since it's all ready - I'm just sort of thinking out loud. And anyway, this'll be a good excuse to implement the configuration file updating scheme we said we wanted to have by 0.3 :)

@eteq
Copy link
Member

eteq commented Feb 7, 2013

Other than that one suggestion, this looks good to me.

@astrofrog
Copy link
Member

This looks good to me!

taldcroft added a commit that referenced this pull request Feb 8, 2013
Make Column or MaskedColumn name arg be a keyword arg name=None
@taldcroft taldcroft merged commit a4065b2 into astropy:master Feb 8, 2013
taldcroft added a commit that referenced this pull request Feb 12, 2013
Make Column or MaskedColumn name arg be a keyword arg name=None
@taldcroft taldcroft deleted the table-name-kwarg branch March 1, 2013 16:35
keflavich pushed a commit to keflavich/astropy that referenced this pull request Oct 9, 2013
Make Column or MaskedColumn name arg be a keyword arg name=None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants