Skip to content

MAINT Use isinstance(x, numbers.Integral) to check for integer dtype - #14004

Merged
thomasjpfan merged 3 commits into
scikit-learn:masterfrom
rth:fix-numbers-integers
Jun 12, 2019
Merged

thomasjpfan merged 3 commits into
scikit-learn:masterfrom
rth:fix-numbers-integers

Conversation

@rth

@rth rth commented May 31, 2019

Copy link
Copy Markdown
Member

Closes #5786

On the discussion whether,

  • isinstance(x, (numbers.Integral, np.integer))
  • isinstance(x, six.integer_types)

should be used for testing integer dtype (including np.integer), a better choice is to use isinstance(x, numbers.Integral) as mentionned in #5786 (comment)

>>> import numpy as np
>>> import numbers
>>> x = np.int64(2)
>>> x
2
>>> type(x)
<class 'numpy.int64'>
>>> isinstance(x, numbers.Integral)
True

@thomasjpfan thomasjpfan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

benchmarks/bench_plot_nmf.py uses from sklearn.decomposition.nmf import INTEGER_TYPES, which should be updated.

@rth

rth commented Jun 3, 2019

Copy link
Copy Markdown
Member Author

Good point, thanks -- fixed.

@jeremiedbb jeremiedbb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm.
(failing tests are unrelated)

@jnothman

Copy link
Copy Markdown
Member

Although I do recall vaguely that some numpy int types behaved differently to others...

@rth

rth commented Jun 11, 2019

Copy link
Copy Markdown
Member Author

Although I do recall vaguely that some numpy int types behaved differently to others...

Maybe some time ago in numpy? Both on latest numpy and v1.11 (latest supported verson) it works for all numpy int types,

>>> import numpy as np
>>> import numbers
>>> isinstance(np.int_(2), numbers.Integral)
True
>>> isinstance(np.intc(2), numbers.Integral)
True
>>> isinstance(np.int64(2), numbers.Integral)
True
>>> isinstance(np.uint8(2), numbers.Integral)
True

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.

isinstance(x, (numbers.Integral, np.integer)) vs isinstance(x, six.integer_types)

4 participants