Anyone else think that the Union type annotation in Python is a code smell that something is too loosely defined?
#python #mypy
@seungjin I write #python code every day, and type hints are a "syntactic sugar" and are not required at all from the interpreter point of view.
For large projects, it's useful to add hints to "follow" what you are doing, and type checkers like #pyright or #mypy can help to find some bugs, by example when calling a method with wrong parameters types.
It also help for the readability of the code.
#ty #mypy - but mypy already compiles itself to c via #mypyc so being faster isn't what I'd be expecting, it will be different and probably find different typing errors since for all the type checkers, it comes down to a matter of opinion. Do you think that will be a runtime error? That's in the future, who knows man.
anyone have some time to help me with a tricky #python #typing and #mypy problem?
it's fully self-contained in https://github.com/cthoyt/python-typing-dilemma. It contains some examples of things I tried, and why they didn't work
it hinges on using PEP-696 defaults in typing.TypeVar, introduced in Python 3.13
I’m busy re-applying this “gradual import” technique on a client project today. Will slowly get them up to a full django-stubs installation.
https://adamj.eu/tech/2022/08/23/python-type-hints-gradually-add-types-for-third-party-packages/
Nous avons tous nos bonnes pratiques lorsqu'il s'agit de créer un nouveau #projet #Python, avec l'utilisation de patterns et d'outils éprouvés : lint avec #ruff et #mypy, hooks avec #precommit, tests avec #pytest, intégration continue #githubactions : https://github.com/neubig/starter-repo
Libre à chaque personne de faire évoluer le porojet selon ses propres goûts et contraintes.
#ChatGPT introduced me to a couple of cool things in #Python today. When you know that a function returns a specific type you can use assert to let #mypy know that. Or you can use the cast() or TypeGuard.
https://chatgpt.com/share/67d9f37a-5434-8009-884a-60eed44951cb
Аннотации типов в Python: коротко о главном
Привет, Хабр! Сегодня рассмотрим, как Python, оставаясь динамически типизированным, может приближаться к строгой типизации. Всё дело в аннотациях типов , которые позволяют явно указывать, какие данные ожидаются в переменных, аргументах функций и возвращаемых значениях. Аннотации сами по себе не заставляют Python проверять типы во время выполнения, но их можно использовать вместе с инструментами статического анализа. В первую очередь мы будем работать с mypy — популярным инструментом, который выявляет ошибки до запуска программы.
New account, so new #introduction. Let's do it again!
I have a #MastersDegree in #ComputerScience and #ComputerEngineering. I went to #NMU for undergrad and #MSU for my graduate degree. I currently work at #UChicago for the #TMWCenter, which focuses on helping young children acquire language (and therefore other learning skills) faster.
In my spare time, I develop #OpenSourceSoftware such as
- a #SphinxDoc extension that embed #Fediverse comments on your page
- a library to talk to #ManifoldMarkets from native #Python
- a #PredictionMarket manager using the above
- a #transpiler from a subset of Python to #OpenStreetMaps's #OverpassQL
- bug fixes to many other projects, including #mypy, #base58, #attrs, #CPython, & more
I spend a fair bit of my time these days on #MathResearch, specifically into the #ThueMorse Sequence and its extensions.
I'm also a hobbyist editor on OpenStreetMap.
#Demisexual, #transfem, and happily engaged to my #enby sweetheart.
We have two cats: #OpheTheLoaf and #MayalaranTheCat (yes, from #StormlightArchive) See here for more on them!
I speak English fluently, Spanish haltingly, and am trying to learn Chinese
#queer #Chicago
The static type annotation system in Python (and associated typecheckers) really turns it into a business-ready language. I'm making some big refactors, and I don't know how I would pull them off without type verification.
So many situations where I change a function signature and discover it's being called ⬅️ ⬅️ ⬅️ ⬅️ ⬅️ ⬅️ ⬅️ ⬅️ alllllll the way over there and would just runtime-error without mypy letting me know the signatures no longer match.
Seriously, static type annotation alone has moved Python from "cute toy language" to "production-worthy" in my professional toolbox.
every time I run mypy and wait for it to finish, I wonder if 2025 will be the year of a red knot announcement/release from @charliermarsh and Astral
I already have a placeholder issue on my cookiecutter template repo: https://github.com/cthoyt/cookiecutter-snekpack/issues/12
@loganer Yes. Python is dynamically typed. I use #mypy a lot to compensate for the weak typing. And it's fairly decent.
But one loses something when one tries to use a programming style meant for a language with different design criteria.
Programming Python in a Lisp or Scheme like style can be very powerful. It's not something we do in C++ a lot, so it feels foreign.
Ironically, I think the parameterized package would actually benefit from being more dynamic in this case.
And one that supported #mypy
Survey on use of types in Python among around 1000 developers. 88% of respondents “Always” or “Often” use Types in their Python code.
Study conducted by Meta, JetBrains, and Microsoft, results on a Google dashboard.
https://engineering.fb.com/2024/12/09/developer-tools/typed-python-2024-survey-meta/
I posted what's probably my last blog post of 2024: Mutually tail-call optimization in Python, passing mypy --strict
type-checking to boot! https://emergent.unpythonic.net/01735585601
Oh dang, my #introduction post is waaay out of date now. Let's do it again!
I have a #MastersDegree in #ComputerScience and #ComputerEngineering. I went to #NMU for undergrad and #MSU for my graduate degree. I currently work at #UChicago for the #TMWCenter, which focuses on helping young children acquire language (and therefore other learning skills) faster.
In my spare time, I develop #OpenSourceSoftware such as
I spend a fair bit of my time these days on #MathResearch, specifically into the #ThueMorse Sequence and its extensions.
I'm also a hobbyist editor on OpenStreetMap.
#Demisexual, #transfem, and happily engaged to my #enby sweetheart.
We have two cats: #OpheTheLoaf and #MayalaranTheCat (yes, from #StormlightArchive)
@cdavies I really really want to hate #python sometimes but can't. The darn thing is so useful. Nothing beats it for anything <1k loc! And no matter what you want to do, it is second or third best choice available.
But as a language for developing systems, it sucks! C-API is terrible (shoutout to #pybind11 and #maturin). Dynamic typing becomes a bane (#mypy helps). For last two days, I am tweaking a #fastapi, I miss rust type safety when refactoring.