Test_generic error in mypy

### What happens

I'm having problems passing the mypy test of my code in relation with this part of code:

    def card_name(k: int = 0, cards: Optional[str] = None,
              count: int = 0, answer: Optional[str] = None) -> None:

### What do you understand or find about that problem

Seems like a problem when you use a empty container or "None" with mypy

### You make any workaround? What did you do?

In this [pipeline](https://gitlab.com/autonomicmind/challenges/-/pipelines/221380782) I got the error

> Incompatible default for argument "cards" (default has type "None", argument has type "List[int]")
so, I read the mypy docs and found this

>  # If you initialize a variable with an empty container or "None"
> # you may have to help mypy a bit by providing a type annotation
> x: List[str] = []
> x: Optional[str] = None
but then the last [pipeline](https://gitlab.com/autonomicmind/challenges/-/pipelines/221402223) fails again with a lot of errors after using the Optional

### Evidences

Errors from the first pipeline
> code/codeabbey/058/juan6630.py:6: error: Incompatible default for argument "cards" (default has type "None", argument has type "List[int]")
> code/codeabbey/058/juan6630.py:7: error: Incompatible default for argument "answer" (default has type "None", argument has type "List[str]")
> Found 2 errors in 1 file (checked 1 source file)

Errors from the last one
> code/codeabbey/058/juan6630.py:21: error: Incompatible types in assignment (expression has type "List[<nothing>]", variable has type "Optional[str]")
> code/codeabbey/058/juan6630.py:22: error: Incompatible types in assignment (expression has type "List[int]", variable has type "Optional[str]")
(and some others)

### I need help with

Since it's my first time trying recursion, maybe my code is the whole problem. But if not, I don't know how to type the reference so mypy can accept it.

Hi, reading the code I find some things to say about that.

First, Optional works like you are saying that you want some kind of type or None, for the case of cards and answer you can say that the pipe says to you that has types of List[<'something'>] and you are declared that you expect str or int, not lists at all.

Second, you should divide your code into parts, that can help you to avoid the need for default values, you can make a main() function and divide the functionalities into small functions, like data_entry() or get_data, another function that solves the challenge, like solve_challenge() and the last function to iterate the data and send the necessary params to solve_challenge()

Functional programming is a way to think of solving things for that reason sometimes is better 5 little functions than 1 big function because you can control better the flow that you are working, makes easier fix a problem if any function fails, etc you can test each function separately or run all functions together.

Take into count that if you can divide it you can reuse the same functions to the same kind of problems, for example, all challenges should get the data, why not think on a way to have a function that can get the data for any challenge with the same data structure?

Try to fix the typing and work with the way that you see that suits better to you, but try to Avoid the use of typings like Any, Optional or Union, because with that you can pass arguments with any kind of type and that isn’t the expected behavior, you should know well what you are sending and receiving.

1 Like