[Python-ideas] Re: Type-hinting dictionaries for an arbitrary number of arbitrary key/value pairs? Counterpart to PEP 589?

2021-10-19 Thread Sebastian M. Ernst
Hi all, thanks for all the feedback. > But this is probably off-topic. I'm not 100% sure what the OP's > proposal was, but as far as I can tell it seems to me that "dict[str, > Number] is the correct usage for static typing" is the answer, > regardless of mypy's ability to process it. Let me

[Python-ideas] Re: Type-hinting dictionaries for an arbitrary number of arbitrary key/value pairs? Counterpart to PEP 589?

2021-10-16 Thread Paul Moore
On Sat, 16 Oct 2021 at 18:01, Steven D'Aprano wrote: > > On Sat, Oct 16, 2021 at 02:49:42PM +0100, Paul Moore wrote: > > > I'd be more likely to just remove the types (the type checking > > equivalent of `#noqa` when you don't agree with what your style > > checker says). > > Type annotations are

[Python-ideas] Re: Type-hinting dictionaries for an arbitrary number of arbitrary key/value pairs? Counterpart to PEP 589?

2021-10-16 Thread Steven D'Aprano
On Sat, Oct 16, 2021 at 02:49:42PM +0100, Paul Moore wrote: > I'd be more likely to just remove the types (the type checking > equivalent of `#noqa` when you don't agree with what your style > checker says). Type annotations are still useful to the human reader, even if the type checker is

[Python-ideas] Re: Type-hinting dictionaries for an arbitrary number of arbitrary key/value pairs? Counterpart to PEP 589?

2021-10-16 Thread Paul Moore
On Sat, 16 Oct 2021 at 14:07, Alex Waygood wrote: > > Indeed — we essentially lie to mypy about the method resolution order for > list, dict, etc (mypy thinks that list directly inherits from > collections.abc.MutableSequence — see the typeshed stub here: >

[Python-ideas] Re: Type-hinting dictionaries for an arbitrary number of arbitrary key/value pairs? Counterpart to PEP 589?

2021-10-16 Thread Alex Waygood
Indeed — we essentially lie to mypy about the method resolution order for list, dict, etc (mypy thinks that list directly inherits from collections.abc.MutableSequence — see the typeshed stub here:

[Python-ideas] Re: Type-hinting dictionaries for an arbitrary number of arbitrary key/value pairs? Counterpart to PEP 589?

2021-10-16 Thread Steven D'Aprano
On Sat, Oct 16, 2021 at 09:54:13AM +0100, Alex Waygood wrote: > > On 16 Oct 2021, at 06:13, Steven D'Aprano wrote: > > Be careful about believing what you are told. > > Indeed, MyPy will correctly raise errors if you assign {None: []} to a > variable annotated with dict[str, Number]. However,

[Python-ideas] Re: Type-hinting dictionaries for an arbitrary number of arbitrary key/value pairs? Counterpart to PEP 589?

2021-10-16 Thread Alex Waygood
> On 16 Oct 2021, at 06:13, Steven D'Aprano wrote: > Be careful about believing what you are told. Indeed, MyPy will correctly raise errors if you assign {None: []} to a variable annotated with dict[str, Number]. However, you'll find that MyPy also raises an error if you assign {'foo': 4} to a

[Python-ideas] Re: Type-hinting dictionaries for an arbitrary number of arbitrary key/value pairs? Counterpart to PEP 589?

2021-10-15 Thread Steven D'Aprano
On Fri, Oct 15, 2021 at 06:17:12PM +0200, Sebastian M. Ernst wrote: > Data = Dict[str, Number] > > @typechecked > def foo(bar: Data): > print(bar) > ``` > > Yes, this is using run-time checks (typeguard), which works just fine. > Only strings as keys and Number objects as values are going

[Python-ideas] Re: Type-hinting dictionaries for an arbitrary number of arbitrary key/value pairs? Counterpart to PEP 589?

2021-10-15 Thread Christopher Barker
On Fri, Oct 15, 2021 at 9:20 AM Sebastian M. Ernst wrote: > PEP 589 introduces typed dictionaries, but for a fixed set of predefined > keys (similar to struct-like constructs in other languages). In > contrast, I am looking for an arbitrary number of typed keys/value pairs. > But that's

[Python-ideas] Re: Type-hinting dictionaries for an arbitrary number of arbitrary key/value pairs? Counterpart to PEP 589?

2021-10-15 Thread Paul Moore
On Fri, 15 Oct 2021 at 18:07, Sebastian M. Ernst wrote: > Ignoring typeguard, my suggestion still stands, although slightly > changed: Annotating a dictionary as described earlier in such a way that > type inference is not required OR in such a way that run-time checkers > have a chance to work

[Python-ideas] Re: Type-hinting dictionaries for an arbitrary number of arbitrary key/value pairs? Counterpart to PEP 589?

2021-10-15 Thread Simão Afonso
Just a pointer related to this, typeguard is abandoned. kttps://github.com/agronholm/typeguard/issues/198 ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org

[Python-ideas] Re: Type-hinting dictionaries for an arbitrary number of arbitrary key/value pairs? Counterpart to PEP 589?

2021-10-15 Thread Sebastian M. Ernst
Hi Paul, all, Am 15.10.21 um 18:49 schrieb Paul Moore: > Mypy correctly rejects this: > [...] interesting. Thanks for trying. > If typeguard doesn't, maybe you need to raise that as a bug against > that project? This is kind of contradicting the design of typeguard. It works on a call level

[Python-ideas] Re: Type-hinting dictionaries for an arbitrary number of arbitrary key/value pairs? Counterpart to PEP 589?

2021-10-15 Thread Paul Moore
Mypy correctly rejects this: ❯ type .\t.py from numbers import Number from typing import Dict Data = Dict[str, Number] def foo(bar: Data): print(bar) bar[1.0] = b'hello' PS 17:48 00:00.008 C:\Work\Scratch\foo ❯ mypy .\t.py t.py:9: error: Invalid index type "float" for "Dict[str,