Re: [Python-Dev] Adding Type[C] to PEP 484

2016-05-14 Thread Guido van Rossum
On Sat, May 14, 2016 at 11:47 AM, Peter Ludemann 
wrote:

> I think that Type[C,D] would mean Union[Type[C], Type[D]], but I'm not
> sure ... I should probably talk to a typing expert about this.
> (Sorry for thinking out loud; but if we decide that Type[C,D] doesn't make
> sense, we need to prohibit it)
>

It sounds iffy enough to prohibit.


> I suppose I'm asking: do we allow new_user(Type[BasicUser, ProUser])?
>

That looks like a call, but Type[] is something to use in the definition.


> Here's a trivial example that off the top of my head makes sense:
>
> BasicOrProUser = TypeVar('BasicOrProUser', BasicUser, ProUser)
> def new_user(name: str, user_factory: Type[BasicOrProuser]) ->
> BasicOrProUser:
> return user_factory(name)
>

That looks reasonable and only has one parameter to Type[].


> Or must the Type[...] item have been defined with a TypeVar(...,
> bound=...), in which case multiple types aren't allowed with Type[...]?
>

You can use whatever you want. If you want to write Type[int] go right
ahead. Even Type[Union[int, str]] should be allowed. There is *usually* a
good reason to use a type variable though -- without one the return type
would be the base class. (But then again maybe that could be what you want,
or you might not even have a return type, e.g. when the thing just gets
thrown into the database.)

--Guido


>
>
> On 14 May 2016 at 11:30, Guido van Rossum  wrote:
>
>> What would Type[C, D] mean?
>>
>> --Guido (mobile)
>> On May 14, 2016 11:21 AM, "Peter Ludemann via Python-Dev" <
>> python-dev@python.org> wrote:
>>
>>> Is Type[C,D] allowed? Or should multiple types be restricted to TypeVar?
>>>
>>> ___
>>> Python-Dev mailing list
>>> Python-Dev@python.org
>>> https://mail.python.org/mailman/listinfo/python-dev
>>> Unsubscribe:
>>> https://mail.python.org/mailman/options/python-dev/guido%40python.org
>>>
>>>
>


-- 
--Guido van Rossum (python.org/~guido)
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Adding Type[C] to PEP 484

2016-05-14 Thread Peter Ludemann via Python-Dev
I think that Type[C,D] would mean Union[Type[C], Type[D]], but I'm not sure
... I should probably talk to a typing expert about this.
(Sorry for thinking out loud; but if we decide that Type[C,D] doesn't make
sense, we need to prohibit it)

I suppose I'm asking: do we allow new_user(Type[BasicUser, ProUser])?

Here's a trivial example that off the top of my head makes sense:

BasicOrProUser = TypeVar('BasicOrProUser', BasicUser, ProUser)
def new_user(name: str, user_factory: Type[BasicOrProuser]) ->
BasicOrProUser:
return user_factory(name)

Or must the Type[...] item have been defined with a TypeVar(...,
bound=...), in which case multiple types aren't allowed with Type[...]?


On 14 May 2016 at 11:30, Guido van Rossum  wrote:

> What would Type[C, D] mean?
>
> --Guido (mobile)
> On May 14, 2016 11:21 AM, "Peter Ludemann via Python-Dev" <
> python-dev@python.org> wrote:
>
>> Is Type[C,D] allowed? Or should multiple types be restricted to TypeVar?
>>
>> ___
>> Python-Dev mailing list
>> Python-Dev@python.org
>> https://mail.python.org/mailman/listinfo/python-dev
>> Unsubscribe:
>> https://mail.python.org/mailman/options/python-dev/guido%40python.org
>>
>>
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Adding Type[C] to PEP 484

2016-05-14 Thread Guido van Rossum
What would Type[C, D] mean?

--Guido (mobile)
On May 14, 2016 11:21 AM, "Peter Ludemann via Python-Dev" <
python-dev@python.org> wrote:

> Is Type[C,D] allowed? Or should multiple types be restricted to TypeVar?
>
> ___
> Python-Dev mailing list
> Python-Dev@python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> https://mail.python.org/mailman/options/python-dev/guido%40python.org
>
>
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Adding Type[C] to PEP 484

2016-05-14 Thread Peter Ludemann via Python-Dev
Is Type[C,D] allowed? Or should multiple types be restricted to TypeVar?
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] file system path protocol PEP

2016-05-14 Thread Nick Coghlan
On 14 May 2016 at 19:36, Sven R. Kunze  wrote:
> Fine example. Thinking naively, I would say, when somebody made an effort to
> write __fspath__, it should be respected. Maybe, that's just me.

Over time, we've generally moved towards treating subclasses as if
they were the base class where protocols are concerned, and
encouraging the use of composition if people don't actually want that
"is-a" behaviour.

For example, compare the int protocol to the last major type protocol
we added, index:

>>> from operator import index
>>> class MyInt(int):
... def __int__(self):
... return 42
... def __index__(self):
... return 42
...
>>> x = MyInt(5)
>>> x
5
>>> int(x)
42
>>> index(x)
5

In the case of retroactively added protocols like operator.index() and
os.fspath(), where inheritance from the relevant builtin type was
previously the only option, retaining that behaviour is also the only
way to avoid a performance regression for those subclasses.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncogh...@gmail.com   |   Brisbane, Australia
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] file system path protocol PEP

2016-05-14 Thread Sven R. Kunze

On 13.05.2016 18:43, Chris Angelico wrote:

https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_Check

https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_CheckExact


Thanks for pointing me at this.
I searched via github and found usages only: 
https://github.com/python/cpython/search?utf8=%E2%9C%93&q=PyUnicode_Check



"Check" accepts subclasses; "CheckExact" doesn't (it's like "type(x)
is str"). The question is, which one SHOULD be being done? What should
this do:

class TmpPath(str):
 def __fspath__(self):
 return "/tmp/"+self
x = TmpPath("foo/bar")
open(x, "w")

Does that respect __fspath__, or respect the fact that it's a string?


Fine example. Thinking naively, I would say, when somebody made an 
effort to write __fspath__, it should be respected. Maybe, that's just me.


Not sure if that can be a source of errors, when somebody adds str as 
baseclass later because the team needs str functionality. At least I 
would expect __fspath__ to still work .


Best,
Sven
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com