Masthash

#MyPy

Marc Gibbons
3 days ago

An admission: I've been recalcitrant toward type annotations in #python since they were introduced (with the notable exception of dataclasses). I begrudgingly used them (w/ #mypy) in a recent project and slowly came around to it – led to catching a few AttributeError vulnerabilities around NoneType values. A bit of stumbling around pytest fixtures returning callbables at first.

Now that I'm working in a legacy codebase, I find myself missing them... #progress ?

scy
1 week ago

I _assumed_ this is because when given a list of files, #mypy handles each one separately, and if you create a file called csv.py (maybe even completely outside of any package whatsoever) and write something like

from csv import DictReader
print(DictReader)

into it, won’t #Python interpret `csv` as _your_ file instead of the standard library?

Turns out, no, it doesn’t. The code above works fine.

In that case I _really_ don’t understand what’s going on with mypy here.

scy
1 week ago

#mypy’s import handling is confusing.

While #Python itself lets me have a module named `csv` inside of a (sub)package (e.g. `convert/csv.py`), and that file can even successfully import the standard library’s _own_ `csv` module, mypy will tell me things like `myproject/convert/csv.py:21: error: Module has no attribute "DictReader"`.

This only happens when giving mypy a _directory_ of files to run on. If you instead run `mypy -p myproject` (i.e. on the _package_), it works. 🤔

Tin Tvrtković
2 weeks ago

While trying to figure out #Mypy docstring conventions to get a PR merged I discovered the Mypy developer guide: https://github.com/python/mypy/wiki/Developer-Guides. Looks like a great starting point for someone getting started with contributing. No docstring conventions mentioned though, I might add that once I work it out.
#python

Ben Clifford
3 weeks ago

Today the mine i am mining in is the regression-tests-that-are-disabled-because-they-stopped-passing mine.

(because #mypy is mad at the test now due to my on-going creeping static checking project)

It's testing a bit of code that I'm expecting new devs to be making significant changes to soon, so it's actually worth fixing...

Ben Clifford
3 weeks ago

dill vs ParamSpec - a battle I wasn't expecting, but apparently here I am.

#mypy #dill #serialization #python

~ashwinvis
3 weeks ago

#TIL executing #mypy as a daemon with an sqlite cache makes it go really fast. Squashed a lot of type errors with that workflow.

https://mypy.readthedocs.io/en/stable/mypy_daemon.html

https://mypy.readthedocs.io/en/stable/additional_features.html#caching-with-mypy-daemon

dmypy run -- --sqlite-cache --use-fine-grained-cache <path>
dmypy check <path>

#python #typing

Daniel Obraczka
4 weeks ago

Is there a common way to typehint slice? Especially for cases, when you want specific things like label-based slicing (think Pandas `.loc` for a string index). Pandas does not have any typehints for keys afaik. #pandas #python #datascience #mypy

ethantyping
1 month ago

Happy Monday! I am looking for work.

I am looking for a role related to compilers ideally, but I'm also capable in numerical computing and back-end roles. I have experience with #Rust 🦀 and #Python 🐍 .

I have a deep knowledge of Python from working on #mypy and CPython itself. The quantum compiler I worked on was a mixed Python/Rust codebase.

I'm looking for remote/hybrid near SF Bay Area.

Github: https://github.com/ethanhs

(boosts appreciated)

#fedijobs #getfedihired #fedihired #jobseeker

Guiguid
2 months ago

@python_discussions
#python : 3 dev tools, #mypy, #ruff and #black, to rule them all !

Csepp 🌢
2 months ago

Trying to track down some stupid type related error in some #Scheme code that a type checker would have caught.
Honestly #MyPy might be a bigger reason for why I often reach for #Python instead of Scheme than library availability.
Maybe I'd like #Racket with its gradual typing extensions, but it is significantly more resource hungry than Python.

Seth Michael Larson
2 months ago

#Mypy 1.2 adds support for native floats and integers to mypyc 👀

https://mypy-lang.blogspot.com/2023/04/mypy-12-released.html

#Python

A function which sums a list of "i64" types from mypy-extensions module representing 64-bit signed integer native types.
Ben Clifford
2 months ago

i am once again wrestling with decorator typing in python

#mypy #python

Juan Luis
2 months ago

Amusing to read this article from @passle about #TypeScript from a #Python perspective: https://dev.to/thepassle/using-typescript-without-compilation-3ko4

Looks like parts of the #JavaScript ecosystem are abandoning TypeScript and putting types in docstrings and header files.

Meanwhile, in Python we are going in the opposite direction: moving types from docstrings to signatures (making them hard to read, quite frankly), and giving up on using stubs because of #MyPy https://github.com/python/mypy/issues/5028

Could we do things differently? #DEVCommunity

What's currently the best way to check type annotations while running unit tests?!?

#mypy is nice, but not enough.

#python #unittest #typechecking

scy
2 months ago

ugghhhh I hate it when I have to implement workarounds just to make #mypy happy.

Today: Classes that implement only __getitem__ are not considered iterable by mypy, even though that’s allowed by PEP 234.

https://github.com/python/mypy/issues/2220

As a workaround, wrap the object you’re iterating over in iter(…).

#Python

César Román :python:
3 months ago

While the world seems to have moved away from #Python2, I am still actively developing packages published on #PyPI, including some #Python3 #stubs packages.

The main challenges I've faced have been some of my "build" dependencies like #black, #pyflakes and #mypy dropping support for Python 2.

Always worrying about the future the next challenge we will face will be #github deprecating #ubuntu 20.04; as of now only 18.04 and 20.04 are the ones including Python 2.7, with 18.04 already deprecated.

Ben Clifford
3 months ago

This One Neat Trick for python decorators (to give optional parameters to python) feels like it needs dependent types to put a type annotation on. or a typechecker plugin.

Ugh.

#Python #MyPy #DependentTypes

Theresa O’Connor
3 months ago

LazyActivityPub: could someone who understands Python type annotations tell me with small words what I'm doing wrong here? https://gist.github.com/hober/b1aa525e1e2bbca1ddeb12d43ba6090a #python #mypy #eli5

Just discovered while reading werkzeug's pyproject.toml that #mypy can read pyproject.toml "now"! (as of 0.900, June two years ago)

Guido was vehemently against that last time I checked (four years ago).

And it's not just a mypy.ini transplant, it's making use of toml to improve the parameter format. Nice!

@python #python
Julia
3 months ago

Python desperately needs an operator that’s just an alias for Optional[T]. So Optional[list[Optional[str]]] becomes something like list[str?]?. Much better.

(Yeah, I should probably rethink my API design instead… 😅) #Python #Mypy

ottO
4 months ago

If you ever have a strange problem where a package method works in the REPL but not in ci ( make or whatever ), and it shows strange behavior like calling dir(object) gives different results check and make sure you haven't let some mypy stubs get into your ci run/test PYTHONPATH, because if they load the stub version of the object/method _last_ that is what the test will use. TIL #mypy #python

James C :python:
4 months ago

Turns out the next version of #mypy is 1.0.0! 🎉🥰🎉 And it’s out already!

Looks like the docs got a significant bump too: https://mypy.readthedocs.io/en/stable/index.html

Silly me for worrying about 0.1000 😅

Ben Clifford
4 months ago

awww poor static typing all upset:

from typing import List, Union
a: Union[List[str], List[int]] = []

$ mypy myfoo.py
myfoo.py:2: error: Incompatible types in assignment (expression has type "List[<nothing>]", variable has type "Union[List[str], List[int]]") [assignment]
Found 1 error in 1 file (checked 1 source file)

#MyPy #Python #Types

Tin Tvrtković
4 months ago

In #Mypy 1.0, you can use `Final` attributes in #attrs classes and Mypy will know they are frozen instance attributes.

So if you use Mypy, this'll get you *overhead-free* frozen classes, but with a bunch of caveats:
* it doesn't work if the attribute has a default (any default), even like `attrs.field` or `attrs.Factory`
* the class won't be hashable by default

These are tricky to solve. The work continues. #python

Tim Kellogg
4 months ago

theres a lot of criticism of #mypy and i have a hard time believing that mock-heavy unit tests are honestly less awkward than type errors. i get it, if you add types to an existing mock-heavy code base, yeah, you’ve only added awkwardness. but on a brand new code base? types with mostly mock-less tests are so much easier, no contest #python

Seth Michael Larson
4 months ago

The #Mypy project has reached a huge milestone, v1.0.0! 🥳 40% performance boost and a bunch of other improvements:

https://mypy-lang.blogspot.com/2023/02/mypy-10-released.html?m=1 #Python

Paolo Melchiorre
4 months ago

MyPy 1.0.0 has been released 🎉

"Mypy is a static type checker for Python. This release includes new features, performance improvements and bug fixes." ✨

Some of them 👇
- the new version numbers will be of form x.y.z 🏷️
- version 1.0 is up to 40% faster than 0.991 ⚡
- it will now generate an error if you use a variable before it’s defined 🐛
- mypyc can now compile Python 3.10 match statements ✅

https://mypy-lang.blogspot.com/2023/02/mypy-10-released.html

#mypy #python #type #check

James C :python:
4 months ago

Current version of #mypy is `0.991` - we're going to be upgrading to `0.1000` or greater soon then?

All version string comparisons in place and tested with "0.1000" vs "0.1"? - everything's going to Just Work ™️ ?

#python #packaging

José A. Alonso
4 months ago

The comprehensive guide to MyPy. ~ Tushar Sadhwani (@sadhlife). https://sadh.life/post/mypy-guide #Python #MyPy

People out here sleepin' on mypyc.

http://mypyc.readthedocs.io/

#Python #mypy #mypyc

Brad Larsen
4 months ago

Type annotations in #Python code help dramatically both to avoid silly bugs and to navigate larger codebases and APIs.

I use #mypy on my Python projects, setting it up as a build-breaking step in CI.

You need to pin the version of mypy that you use to make this setup feasible. Otherwise, updates to mypy can break your project. (Type checking Python code is not well-defined, and always a moving target.)

I also set up mypy to run in my code editor.

I have also had good success finding bugs with #pyright (https://github.com/microsoft/pyright) that mypy missed. But I've only been running that manually, not with CI or editor integration.

Ben Clifford
5 months ago

My back burner side project to get mypy and other type checking into @ParslProject is now explicitly enumerated in one of the grants that funds me, so I'm ramping up on slightly more aggressive rearrangement of code, now the almighty dollar has blessed me.

#MyPy #Python #StaticTyping

I like when I upgrade mypy and get to remove "unused type ignore comment" and "redundant cast" usages. Getting more accurate a little at a time. #python #mypy

dpaste.com
6 months ago

Another week, another thank-you to the creators and maintainers of a valuable open source project.

In this case it's #mypy, static type checker for #Python. This is how you get value from the type-annotation syntax which Python 3 adds, but does not enforce.

Mypy to the rescue! I rely on it, just like I rely on my automated tests. Caught a problem earlier today in fact.

http://mypy-lang.org/

dpaste.com
6 months ago

Another week, another thank-you to the creators and maintainers of a valuable open source project.

In this case it's #mypy, static type checker for #Python. This is how you get value from the type-annotiation syntax which Python 3 adds, but does not enforce.

Mypy to the rescue! I rely on it, just like I rely on my automated tests. Just caught a problem today in fact.

http://mypy-lang.org/

Ed Rivas
6 months ago

Found a weird bug when using #mypy, #django REST Framework, and Django Filter. Any experts willing to take a look? https://github.com/typeddjango/djangorestframework-stubs/issues/299

Matthew Martin ☑ ✅📛
6 months ago

old mypy: oh, man what is that. Please write `cast(str, thing)`

new #mypy, same code: oh please. I'm insulted. Of course that is a string. Remove the redundant cast. This... this... really hurts my feelings.

Ben Clifford
6 months ago

ah the lolz that is `True` typechecks against a function that wants an `int` in Python, because bool is a subclass of int.

explodes my head about once a year

#mypy

Matthew Badger
6 months ago

If you’re a fan of #mypy and complex function decorators, Ivan Levkivskyi just merged this PR which enables ParamSpec and Concatenate use in type aliases. 🥳 https://github.com/python/mypy/pull/14159

Lukas Atkinson
6 months ago

@creepy_owlet @nebelgrau77 Somewhat.

#Python's Unions are a type-system thing and have no runtime representation. Can't instantiate a Union.

Still, you can check cases with isinstance(), and #mypy will warn you if you forget a case. The match syntax is an shortcut for that, so it will work as well:

def check(value: A | B):
match value:
case A(): print("A")
case B(x=x): print(f"B({x}")

But careful: since this is based on isinstance(), you must take inheritance into account.

scy
6 months ago

error: Argument "responses" to "get" of "APIRouter" has incompatible type "Dict[int, Dict[str, Any]]"; expected "Optional[Dict[Union[int, str], Dict[str, Any]]]"

Also entweder #mypy hat Lack gesoffen oder ich.

Olivia Appleton
6 months ago

It's time for a re-#introduction!

I'm a #MastersStudent in #ComputerScience. In my spare time, I develop #OpenSourceSoftware such as
- 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, and more

I'm also a hobbyist editor on OpenStreetMap.

#Bi, #transfem, and happily dating my enby sweetheart

scy
7 months ago

yaaaay #mypy 0.990 is out

it’s stricter than before

all my pipelines are failing

gaaaaahhhhh

Olivia Appleton
7 months ago

It's time for a re-#introduction!

I'm a #MastersStudent in #ComputerScience. In my spare time, I develop #OpenSourceSoftware such as
- 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, and more

I'm also a hobbyist editor on OpenStreetMap.

#Bi, #transfem, and happily dating my enby sweetheart

IMO, mypyc has a potential to become Python 4.0 in the future.

https://github.com/mypyc/mypyc

#Python #python4 #mypy #mypyc

It turns out that mypy doesn't support Python 3.10 yet.

https://github.com/python/mypy/issues/10201

So, no Python 3.10 coding for me yet 🤷‍♂️

#python #typing #mypy #Python310

Periodic reminder that #Python is dynamically typed, but you can enjoy #Python static typing with #Mypy. If you couldn't attend this nice #FOSDEM talk about it, rush to view the recording once it is ready https://fosdem.org/2021/schedule/event/python_mypy/

The other day I did type-first development in Python.

A thing was failing. If you guess that something received a None that didn't expect it, you'd be correct.

I could have written a test for it, but just typing the involved parties was more concise. Add types, see mypy fail, fix the issue, see mypy succeed again.

#python #mypy
Stefano Zacchiroli
3 years ago

I quite like #Python3 with #mypy #gradualtyping, but I still miss proper algebraic data types (ADT) support. I'm giving https://pypi.org/project/algebraic-data-types/ a try. Hit me if you've better suggestions/tips!

Aaron DeVore
4 years ago

All of Python's meta magic really starts to backfire when you try to add static/gradual typing. The language just isn't designed to have types attached, so there's inevitably going to be pain and awkward syntax. #python #mypy

Stefano Zacchiroli
4 years ago

As of today, the full @swheritage #python code base passes #mypy #typechecking. It's been a side-project of mine and I'm supper happy about this milestone. Type annotations are still minimal, but it's a great starting point. https://gph.is/2dUteLx