# Lets use Python type annotations

**URL:** <https://numba.discourse.group/t/lets-use-python-type-annotations/76>\
**Category:** Development\
**Created:** [June 29, 2020, 10:52am UTC](https://numba.discourse.group/t/lets-use-python-type-annotations/76 "2020-06-29T10:52:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![redradist](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/redradist/32/57_2.png) [@redradist](https://numba.discourse.group/u/redradist)\
**Post date:** [June 29, 2020, 10:52am UTC](https://numba.discourse.group/t/lets-use-python-type-annotations/76/1 "2020-06-29T10:52:29Z")

</div>

Guys, I am wondering if `Numba` will support native `CPython` type annotations ?

`CPython` type annotations so powerful that it allow to annotate argument with whatever object:

```python
def sum(i: int, k: int):
    pass

def sum(i: numba.i32, k: numba.i32):
    pass

def for_loop(n: np.array):
    pass

def for_loop(n: np.byte):
    pass

```

Benefits of this is that users in they code should not maintain two separate annotation systems and code would be cleaner 😉

Share your opinion, lets discuss !!

---

<div class="post-metadata">

**Author:** ![luk-f-a](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/luk-f-a/32/56_2.png) [@luk-f-a](https://numba.discourse.group/u/luk-f-a)\
**Post date:** [June 29, 2020, 11:41am UTC](https://numba.discourse.group/t/lets-use-python-type-annotations/76/2 "2020-06-29T11:41:30Z")

</div>

Personally, I would prefer that Numba does not read my variable annotations. The reason is that I currently use mypy to check my code (so I want to annotate), but I don’t want to declare the inputs to my jitted functions.

Numba does an excellent job in figuring the types of the inputs, and compiling correctly based on that. I don’t think there are many cases in which manual annotations are superior (for the `njit` decorator).

Explicit typing is mostly unnecessary, in some case it is harmful (many people fall for the `float64[:]` trap), and in some cases impossible (eg structured arrays created out of csv files).

Numba is different from Cython where annotations improve performance. There’s no performance benefit to annotations in Numba `njit` (`cfunc` is a different problem).

Just my two cents,  
Luk

---

<div class="post-metadata">

**Author:** ![redradist](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/redradist/32/57_2.png) [@redradist](https://numba.discourse.group/u/redradist)\
**Post date:** [June 29, 2020, 11:51am UTC](https://numba.discourse.group/t/lets-use-python-type-annotations/76/3 "2020-06-29T11:51:34Z")

</div>

Disagree, I have experience with many languages (C, C++, Java, C#, Python, even JavaScript and TypeScript) and I have found a lots of benefits with using annotation that allow me to find issues earlier

Python so powerful that they allow express even something like `concepts` in C++ World, meaning they allow to say that argument could be any type that implement some functionality like `Iterator`

Maintaining two annotation systems sometime good, but some time `Numba` could just check if argument annotated with python `int` then apply some optimization and generate code

Anyway I do want only provide annotations as alternative to `njit`

---

<div class="post-metadata">

**Author:** ![luk-f-a](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/luk-f-a/32/56_2.png) [@luk-f-a](https://numba.discourse.group/u/luk-f-a)\
**Post date:** [June 29, 2020, 2:15pm UTC](https://numba.discourse.group/t/lets-use-python-type-annotations/76/4 "2020-06-29T14:15:43Z")

</div>

@redradist, I am not sure on what you are disagreeing with. You said that annotations allow to find issues earlier, and I said that I “currently use mypy to check my code”. So we are in agreement there.

What I said next is that mypy annotations are useful, but Numba explicit typing is most of the time not necessary, ie I would write functions like this:

```python
@njit
def foo(x: int):
    return 1

```

Writing like this is currently valid in Numba, and it that allows mypy to perform static typing, and allows Numba to do type inference on the inputs, and you get the best of both worlds.

Are you disagreeing with my statement that Numba inputs do not need type declarations most of the time?

---

<div class="post-metadata">

**Author:** ![redradist](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/redradist/32/57_2.png) [@redradist](https://numba.discourse.group/u/redradist)\
**Post date:** [June 29, 2020, 2:40pm UTC](https://numba.discourse.group/t/lets-use-python-type-annotations/76/5 "2020-06-29T14:40:52Z")

</div>

Okay, seems like I have not got your point at first time 😉

What actually I propose is that instead of using such code:

```python
@njit("int32(int32)")
def f(x): ...

```

lets write it like this:

```python
@njit
def f(x: numba.int32) -> numba.int32: ...

```

In such way I have to support only one type annotation system

---

<div class="post-metadata">

**Author:** ![luk-f-a](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/luk-f-a/32/56_2.png) [@luk-f-a](https://numba.discourse.group/u/luk-f-a)\
**Post date:** [June 29, 2020, 3:17pm UTC](https://numba.discourse.group/t/lets-use-python-type-annotations/76/6 "2020-06-29T15:17:09Z")

</div>

Thanks for clarifying. How would you express array inputs? I don’t think mypy has a way to write `float64[:,:]`.

One of my points was that `@njit("int32(int32)")` is mostly unnecessary and `@njit` is the better option in many use-cases (probably most use cases). What would happen in your proposal if someone writes

```auto
@njit
def f(x: int) -> int:

```

Would Numba read that annotation or ignore it?

The point is that you don’t need to support two annotations, because `("int32(int32)` can be omitted most of the time.

---

<div class="post-metadata">

**Author:** ![redradist](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/redradist/32/57_2.png) [@redradist](https://numba.discourse.group/u/redradist)\
**Post date:** [June 29, 2020, 3:33pm UTC](https://numba.discourse.group/t/lets-use-python-type-annotations/76/7 "2020-06-29T15:33:54Z")

</div>

`float64[:,:]` here is how it could be done:

```python
class float64: # Custom annotation class
    def __getitem__ (self, item):
        # Some value should be set to identify that float64[:], float64[:,:] or etc.
        return self

float64 = float64()

def for_loop(n: float64[:,:]):
    pass

```

---

<div class="post-metadata">

**Author:** ![luk-f-a](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/luk-f-a/32/56_2.png) [@luk-f-a](https://numba.discourse.group/u/luk-f-a)\
**Post date:** [June 29, 2020, 5:29pm UTC](https://numba.discourse.group/t/lets-use-python-type-annotations/76/8 "2020-06-29T17:29:57Z")

</div>

I’m not expert in the topic but I think mypy still has problem expressing certain types, especially regarding numpy arrays. all these issues are still open:

> <https://github.com/python/typing/issues/513>
>
> I'd like to open a discussion about typing for multi-dimensional arrays in general, and more specifically for NumPy. We have already...

  

> <https://github.com/python/mypy/issues/3345>
>
> Dependent types have been brought up before in a few places: #366, this python-ideas thread, and most recently, #3062. The latter...

  

> <https://github.com/python/typing/issues/516>
>
> As part of the larger project for multi-dimensional arrays (#513), one of the first questions I would like to settle is...

For example, I think integer generics are necessary to describe Numba’s UniTuple type.

---

<div class="post-metadata">

**Author:** ![redradist](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/redradist/32/57_2.png) [@redradist](https://numba.discourse.group/u/redradist)\
**Post date:** [June 29, 2020, 5:35pm UTC](https://numba.discourse.group/t/lets-use-python-type-annotations/76/9 "2020-06-29T17:35:57Z")

</div>

> [@luk-f-a](#):
>
> I’m not expert in the topic but I think mypy still has problem expressing certain types, especially regarding numpy arrays. all these issues are still open:
> 
> [Typing for multi-dimensional arrays · Issue #513 · python/typing · GitHub](https://github.com/python/typing/issues/513)  
> [Integer generics · Issue #3345 · python/mypy · GitHub](https://github.com/python/mypy/issues/3345)  
> [Syntax for typing multi-dimensional arrays · Issue #516 · python/typing · GitHub](https://github.com/python/typing/issues/516)
> 
> For example, I think integer generics are necessary to describe Numba’s UniTuple type.

Python annotations is just an object (any object) that express intent …  
You can just make your’s annotations and when issue with `mypy` annotation will be fixed, just to switch to `mypy` annotation  
I’ve just created one for you 😉
