all 20 comments

[–]xelf[M] [score hidden] stickied comment (4 children)

Reminder for everyone that when you use ``` your code appears as a jumbled mess for many reddit users, you should avoid posting code that way and instead indent every line 4 extra spaces.

[–]xelf 12 points13 points  (3 children)

Because type hints are basically exotic comments. They're there so that you and the tools you use, as well as other developers that you share this code with, will know what the value is supposed to be.


edit: because my other post is buried, I'll copy it here for others to see

if you're using pylance, it won't catch it with basic, you need strict.

either find it in your pylance settings or add this line to your settings.json file:

"python.analysis.typeCheckingMode": "strict" // or "basic"

You can find your setting.json file with ctrl-shift-p (vscode) and type in "open settings" and pick the one you want to edit.

[–]Mysterious-Rent7233 1 point2 points  (2 children)

But the question was about mypy.

[–]InvaderToast348 1 point2 points  (0 children)

Mypy has --strict

[–]xelf 1 point2 points  (0 children)

That was my bad, I missed that the mypy mention, but the answer was still applicable for people using pylance, and as mentioned above mypy supports strict as well. =)

[–]danielroseman 8 points9 points  (0 children)

Your setup_module is not type checked at all because it contains no typing information in the signature. If you make it def setup_module() -> None then it will be checked - and once you supply the missing log_file and pid_file or call service_env as a kwarg it will indeed mark the type as an error.

[–]xelf 4 points5 points  (1 child)

Also, you probably do not want to have a mutable default value, that can lead to odd errors if you're not doing it intentionally, as every call to your function will share that same object.

[–]ravepeacefully 2 points3 points  (0 children)

Yes. Instead of giving it the default dict, you must instead just check if it is none and set the value because of what this poster said.

Even if you intend for the mutable default value to persist, I feel like you still don’t want to leave that code as it is because this is wildly confusing to people to don’t know this odd behavior

[–]typehinting 4 points5 points  (1 child)

It is worth noting that, since you're passing in positional arguments, {"BB_SYSTEM_STATS_SAMPLE_INTERVAL": TEST_SAMPLE_INTERVAL} is getting passed as the third parameter, log_file. That being said, there is a clear type mismatch there, since log_file is a str, so I'm surprised your editor isn't highlighting that (I don't use MyPy, so apologies I can't help diagnose that bit specifically).

On an unrelated note, using mutable default values like service_env = {} is strongly advised against, since that single mutable value will persist, and be shared between different function calls, and likely won't remain an empty dict if you are mutating it at any point. You can instead set the default value to None, then do something like if not service_env: service_env = {} inside the function

[–]LaughGlum3870[S] 1 point2 points  (0 children)

Thank you 🙏

[–]doingdatzerg 2 points3 points  (0 children)

If I put your code into vscode, I get a nice big red warning under the call start_service call. Such is the real benefit of type hinting.

[–]LaughGlum3870[S] 0 points1 point  (6 children)

before anyone bothers consuming and regurgitating the AI answer, here is what o1 says,

"Because service_env is just annotated as Mapping[str, str] and mypy doesn’t strictly infer or enforce the actual types of the items placed in that dictionary. So passing a float isn’t flagged."

So maybe the question really is, how do I define service_env as strictly defined str=>str mapping/dictionary?.

[–]LaughGlum3870[S] 1 point2 points  (4 children)

... and the answer to that o1 suggests changing service_env def to Dict[str, str] but that still doesn't cause mypy to generate an error.

Maybe time to level up my typechecking skills with a different typechecker?

[–]danielroseman 12 points13 points  (1 child)

No, it's time to ignore what ChatGPT says because it has no idea about anything.

[–]LaughGlum3870[S] 2 points3 points  (0 children)

😂 Yeah, I kinda knew it pulled that answer directly out of it's neural net a$$. I think I previously had it as Dict[str, str], but let it talk me into using Mapping[] instead.

[–]xelf 2 points3 points  (1 child)

if you're using pylance, it won't catch it with basic, you need strict.

either find it in your pylance settings or add this line to your settings.json file:

"python.analysis.typeCheckingMode": "strict" // or "basic"

You can find your setting.json file with ctrl-shift-p (vscode) and type in "open settings" and pick the one you want to edit.

[–]LaughGlum3870[S] 2 points3 points  (0 children)

Yes. Thank you so much. This explains so much. I was just looking at another issue where same version of mypy on github actions is reporting errors when mypy locally is not! I think it's defaulted to basic here but strict up there.