Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

numpy 2+ #125

Open
SanPen opened this issue Oct 30, 2024 · 1 comment
Open

numpy 2+ #125

SanPen opened this issue Oct 30, 2024 · 1 comment

Comments

@SanPen
Copy link

SanPen commented Oct 30, 2024

Hi,

I use nptyping, and I needed it to work with numpy 2.x, hence I just copied the code in my repo and referenced it.

The types from the file typing_.py that work for both new and old numpy are:

try:
    from typing import (  # type: ignore[attr-defined,misc] # pylint: disable=unused-import
        Literal,
        TypeAlias,
        TypeGuard,
        final,
    )
except ImportError:  # pragma: no cover
    from typing_extensions import (  # type: ignore[attr-defined,misc]
        Literal,
        TypeAlias,
        TypeGuard,
        final,
    )

from typing import Tuple, Union

import numpy as np

ShapeExpression: TypeAlias = str
StructureExpression: TypeAlias = str
DType: TypeAlias = Union[np.generic, StructureExpression]
ShapeTuple: TypeAlias = Tuple[int, ...]

Number = np.number
Bool = np.bool_
Bool8 = np.bool_
Obj = np.object_  # Obj is a common abbreviation and should be usable.
Object = np.object_
Object0 = np.object_
Datetime64 = np.datetime64
Integer = np.integer
SignedInteger = np.signedinteger
Int8 = np.int8
Int16 = np.int16
Int32 = np.int32
Int64 = np.int64
Byte = np.byte
Short = np.short
IntC = np.intc
IntP = np.intp
Int0 = np.int8
Int = np.integer  # Int should translate to the "generic" int type.
Int_ = np.int_
LongLong = np.longlong
Timedelta64 = np.timedelta64
UnsignedInteger = np.unsignedinteger
UInt8 = np.uint8
UInt16 = np.uint16
UInt32 = np.uint32
UInt64 = np.uint64
UByte = np.ubyte
UShort = np.ushort
UIntC = np.uintc
UIntP = np.uintp
UInt0 = np.uint
UInt = np.uint
ULongLong = np.ulonglong
Inexact = np.inexact
Floating = np.floating
Float16 = np.float16
Float32 = np.float32
Float64 = np.float64
Half = np.half
Single = np.single
Double = np.double
Float = np.float64
LongDouble = np.longdouble
LongFloat = np.longdouble
ComplexFloating = np.complexfloating
Complex64 = np.complex64
Complex128 = np.complex128
CSingle = np.csingle
SingleComplex = np.complex64
CDouble = np.cdouble
Complex = np.complex128
CFloat = np.complex128
CLongDouble = np.clongdouble
CLongFloat = np.clongdouble
LongComplex = np.clongdouble
Flexible = np.flexible
Void = np.void
Void0 = np.void
Character = np.character
Bytes = np.bytes_
Str = np.str_
String = np.bytes_
Bytes0 = np.bytes_
Unicode = np.str_
Str0 = np.str_

dtypes = [
    (Number, "Number"),
    (Bool, "Bool"),
    (Bool8, "Bool8"),
    (Obj, "Obj"),
    (Object, "Object"),
    (Object0, "Object0"),
    (Datetime64, "Datetime64"),
    (Integer, "Integer"),
    (SignedInteger, "SignedInteger"),
    (Int8, "Int8"),
    (Int16, "Int16"),
    (Int32, "Int32"),
    (Int64, "Int64"),
    (Byte, "Byte"),
    (Short, "Short"),
    (IntC, "IntC"),
    (IntP, "IntP"),
    (Int0, "Int0"),
    (Int, "Int"),
    (LongLong, "LongLong"),
    (Timedelta64, "Timedelta64"),
    (UnsignedInteger, "UnsignedInteger"),
    (UInt8, "UInt8"),
    (UInt16, "UInt16"),
    (UInt32, "UInt32"),
    (UInt64, "UInt64"),
    (UByte, "UByte"),
    (UShort, "UShort"),
    (UIntC, "UIntC"),
    (UIntP, "UIntP"),
    (UInt0, "UInt0"),
    (UInt, "UInt"),
    (ULongLong, "ULongLong"),
    (Inexact, "Inexact"),
    (Floating, "Floating"),
    (Float16, "Float16"),
    (Float32, "Float32"),
    (Float64, "Float64"),
    (Half, "Half"),
    (Single, "Single"),
    (Double, "Double"),
    (Float, "Float"),
    (LongDouble, "LongDouble"),
    (LongFloat, "LongFloat"),
    (ComplexFloating, "ComplexFloating"),
    (Complex64, "Complex64"),
    (Complex128, "Complex128"),
    (CSingle, "CSingle"),
    (SingleComplex, "SingleComplex"),
    (CDouble, "CDouble"),
    (Complex, "Complex"),
    (CFloat, "CFloat"),
    (CLongDouble, "CLongDouble"),
    (CLongFloat, "CLongFloat"),
    (LongComplex, "LongComplex"),
    (Flexible, "Flexible"),
    (Void, "Void"),
    (Void0, "Void0"),
    (Character, "Character"),
    (Bytes, "Bytes"),
    (String, "String"),
    (Str, "Str"),
    (Bytes0, "Bytes0"),
    (Unicode, "Unicode"),
    (Str0, "Str0"),
]

name_per_dtype = dict(dtypes)
dtype_per_name = {name: dtype for dtype, name in dtypes}

Would be cool if you fix it and push, otherwise here it is for others to use.

@maxsegl
Copy link

maxsegl commented Nov 13, 2024

Would make my life also easier!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants