[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-24 Thread Sebastian Rittau
Am 24.04.2021 um 01:26 schrieb Gregory P. Smith: Practically speaking, one issue I have is how easy it is to write isinstance or issubclass checks. It has historically been much more difficult to write and maintain a check that something looks like a duck.  `if hasattr(foo, 'close') and

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-24 Thread Antoine Pitrou
On Thu, 22 Apr 2021 12:47:42 -0300 Luciano Ramalho wrote: > > Go is by far the most successful statically typed language created so > far in the 21st century, First, it seems gratuitous to restrict your search to "created so far in the 21st century". I suppose that allows you to eliminate

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-24 Thread Antoine Pitrou
On Wed, 21 Apr 2021 12:36:34 -0700 Christopher Barker wrote: > > But that's not what duck typing is (at least to me :-) ) For a given > function, I need the passed in object to quack (and yes, I need that quack > to sound like a duck) -- but I usually don't care whether that object > waddles

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-24 Thread Stephen J. Turnbull
Chris Angelico writes: > On Sat, Apr 24, 2021 at 10:14 AM Nick Coghlan wrote: > > Duck typing usually falls squarely into the EAFP category. > > Isn't it orthogonal? Not really. If you ask 'thing' to 'thing[0]', you don't find out whether it is a list or a dict (or any number of other

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-23 Thread Chris Angelico
On Sat, Apr 24, 2021 at 10:14 AM Nick Coghlan wrote: > > > > On Sat, 24 Apr 2021, 10:02 am Skip Montanaro, > wrote: >> >> >>> Practically speaking, one issue I have is how easy it is to write >>> isinstance or issubclass checks. It has historically been much more >>> difficult to write and

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-23 Thread Nick Coghlan
On Fri, 23 Apr 2021, 12:34 pm Inada Naoki, wrote: > > I think using ABC to distinguish sequence or mapping is a bad idea. > > There are three policies: > > a) Use duck-typing; just us it as sequence. No type check at all. > b) Use strict type checking; isinstance(x, list) / isinstance(x, (list,

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-23 Thread Nick Coghlan
On Sat, 24 Apr 2021, 10:02 am Skip Montanaro, wrote: > > Practically speaking, one issue I have is how easy it is to write >> isinstance or issubclass checks. It has historically been much more >> difficult to write and maintain a check that something looks like a duck. >> >> `if hasattr(foo,

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-23 Thread Skip Montanaro
> Practically speaking, one issue I have is how easy it is to write > isinstance or issubclass checks. It has historically been much more > difficult to write and maintain a check that something looks like a duck. > > `if hasattr(foo, 'close') and hasattr(foo, 'seek') and hasattr(foo, > 'read'):`

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-23 Thread Gregory P. Smith
When reading this, I wrote most of it early and left a draft to bake Then deleted a ton of it after other people replied. I'm conscious that my terminology might be all over the map. Keep that in mind before hitting reply. It'll take me a while to digest and pedantically use Luciano's terms,

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-23 Thread Caleb Donovick
We can add .keys() to Mapping to distinguish Mapping and Sequence. But it is breaking change, of course. We shouldn’t change it. We could use the presence of .keys in the subclasses hook only after first checking explicit cases (i.e. actual subclass or has been registered). Yes this would break

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-22 Thread Inada Naoki
On Fri, Apr 23, 2021 at 10:33 AM Chris Angelico wrote: > > On Fri, Apr 23, 2021 at 11:22 AM Larry Hastings wrote: > > > > > > On 4/20/21 10:03 AM, Mark Shannon wrote: > > > > If you guarded your code with `isinstance(foo, Sequence)` then I could not > > use it with my `Foo` even if my `Foo`

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-22 Thread Chris Angelico
On Fri, Apr 23, 2021 at 11:22 AM Larry Hastings wrote: > > > On 4/20/21 10:03 AM, Mark Shannon wrote: > > If you guarded your code with `isinstance(foo, Sequence)` then I could not > use it with my `Foo` even if my `Foo` quacked like a sequence. I was forced > to use nominal typing; inheriting

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-22 Thread Larry Hastings
On 4/20/21 10:03 AM, Mark Shannon wrote: If you guarded your code with `isinstance(foo, Sequence)` then I could not use it with my `Foo` even if my `Foo` quacked like a sequence. I was forced to use nominal typing; inheriting from Sequence, or explicitly registering as a Sequence. If I'm

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-22 Thread Paul Moore
On Thu, 22 Apr 2021 at 21:40, Adrian Freund wrote: > If I understand correctly your concerns with inferring return types for > inferred protocols are that it might be to restrictive and prevent > gradual typing. Here are some examples to show how gradual typing would > still work. OK, I have no

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-22 Thread Adrian Freund
On 4/22/21 5:00 PM, Paul Moore wrote: > On Thu, 22 Apr 2021 at 15:22, Adrian Freund wrote: >> On April 22, 2021 3:15:27 PM GMT+02:00, Paul Moore >> wrote: >>> but that's *absolutely* as far as I'd want to go. Note in particular >>> that I don't want to constrain the return value >> The problem

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-22 Thread Luciano Ramalho
On Thu, Apr 22, 2021 at 5:43 AM Chris Angelico wrote: > File-like objects are used VERY frequently in the stdlib, and actual > open file objects have quite a large interface. The use-case is a > pretty real one: "if I were to create a simulant file object to pass > to json.load(), what methods do

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-22 Thread Luciano Ramalho
On Thu, Apr 22, 2021 at 6:57 AM Paul Moore wrote: > > On Thu, 22 Apr 2021 at 09:46, Chris Angelico wrote: > > Maybe in some cases, the "smaller protocols" option is practical, but > > it would need to have a useful name. For instance, if it needs to be > > readable, iterable, closeable, and

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-22 Thread Luciano Ramalho
On Wed, Apr 21, 2021 at 7:31 PM Paul Bryan wrote: > As demonstrated, protocols don't get us there because duck typing isn't a > matter of having an object exhibit all of the attributes of a duck, but > rather some subset of attributes to be used by the consumer. I want this duck > to quack;

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-22 Thread Luciano Ramalho
On Wed, Apr 21, 2021 at 4:44 PM Christopher Barker wrote: >> You say this like it's a bad thing, but how is this avoidable, even in >> principle? Structural typing lets you check whether Foo is duck-shaped >> -- has appropriate attribute names, etc. But quacking like a duck is >> harder: you also

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-22 Thread Terry Reedy
On 4/22/2021 9:15 AM, Paul Moore wrote: Absolutely, I see no problem with "use duck typing for this argument" being opt-in. As in x: 'duck'? or x: '!', where '!' means 'infer it!', or from typing import Infer ... x: Infer ? Ditto for -> ? -- Terry Jan Reedy

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-22 Thread Christopher Barker
On Thu, Apr 22, 2021 at 10:47 AM Matthew Einhorn wrote: > In PyCharm, the above code will result in it highlighting the number `12` with the following warning: "Type 'int' doesn't have expected attribute 'close'" Which gives yet another use for type hints: helping out IDEs. > If instead you

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-22 Thread Matthew Einhorn
On Thu, Apr 22, 2021, at 1:01 PM, Brett Cannon wrote: > > > On Thu, Apr 22, 2021 at 4:11 AM Paul Moore wrote: >> On Thu, 22 Apr 2021 at 11:21, Paul Moore wrote: >> > >> > On Thu, 22 Apr 2021 at 11:06, Chris Angelico wrote: >> > > >> > > Someone will likely correct me if this is inaccurate,

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-22 Thread Brett Cannon
On Thu, Apr 22, 2021 at 4:11 AM Paul Moore wrote: > On Thu, 22 Apr 2021 at 11:21, Paul Moore wrote: > > > > On Thu, 22 Apr 2021 at 11:06, Chris Angelico wrote: > > > > > > Someone will likely correct me if this is inaccurate, but my > > > understanding is that that's exactly what you get if

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-22 Thread Adrian Freund
On April 22, 2021 3:15:27 PM GMT+02:00, Paul Moore wrote: >On Thu, 22 Apr 2021 at 13:23, Adrian Freund wrote: >> >> According to PEP 484 all missing annotations in checked functions should be >> handled as Any. Any is compatible with all types. > >Yep, that's what I understood to be the

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-22 Thread Adrian Freund
According to PEP 484 all missing annotations in checked functions should be handled as Any. Any is compatible with all types. I think from a technical standpoint it should be possible to infer protocols for arguments for most functions, but there are some edge cases where this would not be

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-22 Thread Sebastian Rittau
Am 22.04.21 um 10:42 schrieb Chris Angelico: File-like objects are used VERY frequently in the stdlib, and actual open file objects have quite a large interface. The use-case is a pretty real one: "if I were to create a simulant file object to pass to json.load(), what methods do I need?".

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-22 Thread Luciano Ramalho
Please let's not try to make Python a "typesafe" language. The success of Python owes a lot to the fact that duck typing is approachable, flexible and powerful. Even if you advocate static typing, I think it's a very good idea to limit the ambition of the type system if you want to keep most

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-22 Thread Paul Moore
On Thu, 22 Apr 2021 at 15:22, Adrian Freund wrote: > > On April 22, 2021 3:15:27 PM GMT+02:00, Paul Moore > wrote: > >but that's *absolutely* as far as I'd want to go. Note in particular > >that I don't want to constrain the return value > The problem is that this isn't enough to have a type

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-22 Thread Paul Moore
On Thu, 22 Apr 2021 at 13:23, Adrian Freund wrote: > > According to PEP 484 all missing annotations in checked functions should be > handled as Any. Any is compatible with all types. Yep, that's what I understood to be the case. > I think from a technical standpoint it should be possible to

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-22 Thread Paul Moore
On Thu, 22 Apr 2021 at 11:21, Paul Moore wrote: > > On Thu, 22 Apr 2021 at 11:06, Chris Angelico wrote: > > > > Someone will likely correct me if this is inaccurate, but my > > understanding is that that's exactly what you get if you just don't > > give a type hint. The point of type hints is to

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-22 Thread Paul Moore
On Thu, 22 Apr 2021 at 11:06, Chris Angelico wrote: > > On Thu, Apr 22, 2021 at 7:53 PM Paul Moore wrote: > > I wonder whether type checkers could handle a "magic" type (let's call > > it DuckTyped for now :-)) which basically means "infer a protocol > > based on usage in this function". So if I

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-22 Thread Chris Angelico
On Thu, Apr 22, 2021 at 7:53 PM Paul Moore wrote: > I wonder whether type checkers could handle a "magic" type (let's call > it DuckTyped for now :-)) which basically means "infer a protocol > based on usage in this function". So if I do: > > def my_fn(f: DuckTyped): > with f: > data

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-22 Thread Paul Moore
On Thu, 22 Apr 2021 at 09:46, Chris Angelico wrote: > Maybe in some cases, the "smaller protocols" option is practical, but > it would need to have a useful name. For instance, if it needs to be > readable, iterable, closeable, and autocloseable via > __enter__/__exit__, that's ... uhh a

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-22 Thread Chris Angelico
On Thu, Apr 22, 2021 at 5:03 PM Ryan Gonzalez wrote: > > On Apr 21, 2021, 5:29 PM -0500, Paul Bryan , wrote: > > As demonstrated, protocols don't get us there because duck typing isn't a > matter of having an object exhibit all of the attributes of a duck, but > rather some subset of attributes

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-22 Thread Ryan Gonzalez
On Apr 21, 2021, 5:29 PM -0500, Paul Bryan , wrote: > As demonstrated, protocols don't get us there because duck typing isn't a > matter of having an object exhibit all of the attributes of a duck, but > rather some subset of attributes to be used by the consumer. I want this duck > to quack;

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-21 Thread Carol Willing
Luciano, Thank you for such a thoughtful and eloquent response. Your wisdom is definitely appreciated, and I agree this is an opportunity to go forward with more clarity. I'm so proud of the Python dev community for their thoughtful and respectful conversation. All, Without the efforts of Larry,

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-21 Thread Caleb Donovick
My personal motivating example for PEP 637 was shorthand for protocols. `x: Protocol[foo=int, bar=Callable[str, int]]` could say x has attribute foo which is an int and method bar from str to int. On Wed, Apr 21, 2021 at 4:23 PM Paul Bryan wrote: > I agree, that's duck typing with a protocol,

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-21 Thread Paul Bryan
I agree, that's duck typing with a protocol, and precisely the tedious type style I would want to avoid. I don't know what would be a good suggestion. Something where you reference a "fully equipped" type and cherry pick the attributes you want? Something like `Protocol[Duck, ("quack",

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-21 Thread Jelle Zijlstra
El mié, 21 abr 2021 a las 15:30, Paul Bryan () escribió: > As demonstrated, protocols don't get us there because duck typing isn't a > matter of having an object exhibit all of the attributes of a duck, but > rather some subset of attributes to be used by the consumer. I want this > duck to

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-21 Thread Paul Bryan
As demonstrated, protocols don't get us there because duck typing isn't a matter of having an object exhibit all of the attributes of a duck, but rather some subset of attributes to be used by the consumer. I want this duck to quack; someone else will want it to waddle. I don't see how type hints

[Python-Dev] Re: Keeping Python a Duck Typed Language

2021-04-21 Thread David Antonini
at a glance if what you have in an innocently looking `except Exception` is correct or not. In my async/await code I'd have to always check the `__group__` attribute to make sure it's not an exception group in disguise. So while you're "simplifying" the proposal by removing a coupl

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-21 Thread Christopher Barker
Thanks Mark for posting this. I know some of us are uneasy about the pace of the typing train On Tue, Apr 20, 2021 at 11:20 AM Nathaniel Smith wrote: > > If you guarded your code with `isinstance(foo, Sequence)` then I could > > not use it with my `Foo` even if my `Foo` quacked like a

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-21 Thread Sebastian Rittau
Am 20.04.2021 um 19:03 schrieb Mark Shannon: PEP 544 supports structural typing, but to declare a structural type you must inherit from Protocol. That smells a lot like nominal typing to me. I'm not sure what inheriting from Protocol has to do with nominal typing, even though I would

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-20 Thread Luciano Ramalho
Now, given the terms I used in the last e-mail, it does worry me that the push towards supporting STATIC TYPING in Python sometimes ignores the needs of people who use the other typing disciplines. For example, Pydantic is an example of using type hints for GOOSE TYPING. And it alwo worries me

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-20 Thread Mark Shannon
Hi Luciano, On 20/04/2021 11:35 pm, Luciano Ramalho wrote: I am not taking sides now, but I want to share with you a useful diagram to reason about typing support in Python. I struggled to explain what Python offers until I came up with this diagram:

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-20 Thread Luciano Ramalho
I am not taking sides now, but I want to share with you a useful diagram to reason about typing support in Python. I struggled to explain what Python offers until I came up with this diagram: https://standupdev.com/wiki/doku.php?id=python_protocols#the_typing_map The Typing Map has two

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-20 Thread Eric Casteleijn
On Tue, Apr 20, 2021 at 10:03 AM Mark Shannon wrote: > ... > PEP 544 supports structural typing, but to declare a structural type you > must inherit from Protocol. > That smells a lot like nominal typing to me. > Note that to implement a protocol you do not have to inherit from anything. You

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-20 Thread Larry Hastings
On 4/20/21 10:03 AM, Mark Shannon wrote: Then came PEP 563 and said that if you wanted to access the annotations of an object, you needed to call typing.get_type_hints() to get annotations in a meaningful form. This smells a bit like enforced static typing to me. I'm working to address

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-20 Thread Nathaniel Smith
On Tue, Apr 20, 2021 at 10:07 AM Mark Shannon wrote: > > Hi everyone, > > Once upon a time Python was a purely duck typed language. > > Then came along abstract based classes, and some nominal typing starting > to creep into the language. > > If you guarded your code with `isinstance(foo,

[Python-Dev] Re: Keeping Python a Duck Typed Language.

2021-04-20 Thread Chris Angelico
On Wed, Apr 21, 2021 at 3:04 AM Mark Shannon wrote: > Then came type hints. PEP 484 explicitly said that type hints were > optional and would *always* be optional. > > Then came along many typing PEPs that assumed that type hints would only > used for static typing, making static typing a bit