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

Improve Int and Float type stubs #1656

Merged
merged 2 commits into from
Jun 30, 2022
Merged

Conversation

mdickinson
Copy link
Member

@mdickinson mdickinson commented Jun 29, 2022

This PR improves the type stubs for Int and Float, being more accurate about what's accepted for those trait types.

We need SupportsIndex, which is only available in typing from Python 3.8 onwards. So I've added typing-extensions as a dependency for traits-stubs. We can drop the dependency once we no longer support Python 3.6 and Python 3.7.

Copy link
Contributor

@corranwebster corranwebster left a comment

Choose a reason for hiding this comment

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

Generally looks good. A couple of questions.

@@ -11,6 +11,18 @@
from traits.api import Float, HasTraits


class HasIndex:
Copy link
Contributor

Choose a reason for hiding this comment

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

Dumb question (and probably not for this PR in any case): do we have a test for this sort of thing in traits test suite?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I'm fairly sure we do. :-) I'll check.

Copy link
Member Author

Choose a reason for hiding this comment

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

Int:

class IntegerLike(object):
def __index__(self):
return 42

Float:

class IntegerLike:
def __init__(self, value):
self._value = value
def __index__(self):
return self._value
# Python versions < 3.8 don't support conversion of something with __index__
# to float.
try:
float(IntegerLike(3))
except TypeError:
float_accepts_index = False
else:
float_accepts_index = True
class MyFloat(object):
def __init__(self, value):
self._value = value
def __float__(self):
return self._value

Copy link
Member Author

Choose a reason for hiding this comment

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

.. which reminds me that SupportsIndex isn't actually quite right for the Float trait type with Python < 3.8. I don't think I care enough to try to distinguish. For the use-cases we care about (e.g., NumPy int types), those types implement __float__ anyway.

@@ -57,7 +66,7 @@ class _BaseInt(_TraitType[_T, int]):
...


class BaseInt(_BaseInt[int]):
class BaseInt(_TraitType[SupportsIndex, int]):
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we still need _BaseInt and _BaseFloat?

Copy link
Member Author

Choose a reason for hiding this comment

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

They're still used in the CInt and CFloat trait types, I think. I looked at dealing with those ones too, but decided to limit the PR to avoid scope creep.

@mdickinson mdickinson merged commit 26e6aaa into main Jun 30, 2022
@mdickinson mdickinson deleted the stubs/better-int-and-float-stubs branch June 30, 2022 07:08
mdickinson added a commit that referenced this pull request Aug 9, 2022
This PR improves the type stubs for Int and Float, being more accurate about what's accepted for those trait types.

We need SupportsIndex, which is only available in typing from Python 3.8 onwards. So I've added typing-extensions as a dependency for traits-stubs. We can drop the dependency once we no longer support Python 3.6 and Python 3.7.

(cherry picked from commit 26e6aaa)
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.

2 participants