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

Restrict PrefixList signature to accept a list or tuple of values #1142

Merged
merged 8 commits into from
May 22, 2020

Conversation

kitchoi
Copy link
Contributor

@kitchoi kitchoi commented May 22, 2020

Closes #1137

This forces PrefixList to have at least exactly one positional argument, and that legal values must be provided via a list or a tuple.

Checklist

  • Tests
  • Update API reference (docs/source/traits_api_reference)
  • Update User manual (docs/source/traits_user_manual)
  • Update type annotation hints in traits-stubs

self.values = values[:]
def __init__(self, values, **metadata):
if not isinstance(values, SequenceTypes):
raise ValueError(
Copy link
Member

Choose a reason for hiding this comment

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

I think this is a TypeError (wrong sort or number of parameters) rather than a ValueError (right sort or number of parameters, but bad values).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, it would be consistent with the test above!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And the check was isinstance, so TypeError makes more sense indeed.

Either all legal string values for the enumeration, or a single list
or tuple of legal string values.
values
A single list or tuple of legal string values.
Copy link
Member

Choose a reason for hiding this comment

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

I'd be happy to modify this docstring not to advertise the tuple support; that support seems misplaced to me, and I'm not sure I want to promise such support going forward.

def __init__(self, values, **metadata):
if not isinstance(values, SequenceTypes):
raise TypeError(
"Legal values should be provided via a list, "
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The mention of tuple is excluded here...making the error message inconsistent with the actual check.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe what we want to do is mimic what's in BaseEnum: do an explicit isinstance check to exclude str, bytes and bytearray, but otherwise do no checking and rely on duck typing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now I am thinking we should just check isinstance(values, list).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Race condition: I did not see @mdickinson's message when I posted mine. Duck-typing with the exclusion of strings and friends would do too.

Copy link
Member

@mdickinson mdickinson May 22, 2020

Choose a reason for hiding this comment

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

So looking at the code, I think we could replace self.values = values[:] in the __init__ with self.values = list(values) (after doing the check to exclude str and friends).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@kitchoi kitchoi requested a review from mdickinson May 22, 2020 12:16
mdickinson added a commit that referenced this pull request May 22, 2020
* Add changelog for 6.1, release summary is still missing

* Comestic fixes

* Move Misc item to the bottom

* Add placeholder for release date; add recent PRs

* Stash partial work before context switch

* More updates; move #1058 to correct section; add #1127

* More updates; mostly formatting

* Update CHANGES.rst

Co-authored-by: Kit Choi <kitchoi@users.noreply.github.com>

* More tweaks to wording and content

* More wording tweaks

* Use short form for View reference

* Make use of the intersphinx mapping

* Rewordings

* Remove broken links

* Add recent PRs; fix some long lines

* Combine duplicate change entries for Traits / TraitsUI compatibility

* Add note about default versus default_value for Either and Union

* Add back note that Either will eventually be deprecated.

* Remove some duplicate entries; add some missing ones

* Ordering

* More PRs (sneakily adding #1142 before it's actually merged)

Co-authored-by: Kit Yan Choi <kchoi@enthought.com>
Co-authored-by: Kit Choi <kitchoi@users.noreply.github.com>
Copy link
Member

@mdickinson mdickinson left a comment

Choose a reason for hiding this comment

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

LGTM

@mdickinson mdickinson merged commit e60b506 into master May 22, 2020
@mdickinson mdickinson deleted the 1137-prefix-list-signature branch May 22, 2020 12:20
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.

Restrict PrefixList to accept only a list of values
2 participants