Skip to content

Commit

Permalink
ENH: Add 'ulong' to sctypeDict (#21151)
Browse files Browse the repository at this point in the history
* ENH: Add 'ulong' to sctypeDict

Closes #21063

* TST: Add a test for np.dtype("ulong")

* REV: Don't add new attribute np.ulong
  • Loading branch information
gmgunter committed Mar 14, 2022
1 parent fc90448 commit 71e7620
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
11 changes: 10 additions & 1 deletion numpy/core/_type_aliases.py
Expand Up @@ -172,7 +172,7 @@ def _set_up_aliases():
allTypes[alias] = allTypes[t]
sctypeDict[alias] = sctypeDict[t]
# Remove aliases overriding python types and modules
to_remove = ['ulong', 'object', 'int', 'float',
to_remove = ['object', 'int', 'float',
'complex', 'bool', 'string', 'datetime', 'timedelta',
'bytes', 'str']

Expand All @@ -182,6 +182,15 @@ def _set_up_aliases():
del sctypeDict[t]
except KeyError:
pass

# Additional aliases in sctypeDict that should not be exposed as attributes
attrs_to_remove = ['ulong']

for t in attrs_to_remove:
try:
del allTypes[t]
except KeyError:
pass
_set_up_aliases()


Expand Down
5 changes: 5 additions & 0 deletions numpy/core/tests/test_dtype.py
Expand Up @@ -1381,6 +1381,11 @@ def test_keyword_argument():
assert np.dtype(dtype=np.float64) == np.dtype(np.float64)


def test_ulong_dtype():
# test for gh-21063
assert np.dtype("ulong") == np.dtype(np.uint)


class TestFromDTypeAttribute:
def test_simple(self):
class dt:
Expand Down
7 changes: 7 additions & 0 deletions numpy/core/tests/test_numerictypes.py
Expand Up @@ -436,6 +436,13 @@ def test_longdouble(self):
assert_(np.sctypeDict['f8'] is not np.longdouble)
assert_(np.sctypeDict['c16'] is not np.clongdouble)

def test_ulong(self):
# Test that 'ulong' behaves like 'long'. np.sctypeDict['long'] is an
# alias for np.int_, but np.long is not supported for historical
# reasons (gh-21063)
assert_(np.sctypeDict['ulong'] is np.uint)
assert_(not hasattr(np, 'ulong'))


class TestBitName:
def test_abstract(self):
Expand Down
2 changes: 1 addition & 1 deletion numpy/typing/_char_codes.py
Expand Up @@ -30,7 +30,7 @@
_UShortCodes = Literal["ushort", "H", "=H", "<H", ">H"]
_UIntCCodes = Literal["uintc", "I", "=I", "<I", ">I"]
_UIntPCodes = Literal["uintp", "uint0", "P", "=P", "<P", ">P"]
_UIntCodes = Literal["uint", "L", "=L", "<L", ">L"]
_UIntCodes = Literal["ulong", "uint", "L", "=L", "<L", ">L"]
_ULongLongCodes = Literal["ulonglong", "Q", "=Q", "<Q", ">Q"]

_HalfCodes = Literal["half", "e", "=e", "<e", ">e"]
Expand Down

0 comments on commit 71e7620

Please sign in to comment.