[Python-ideas] Proposal: Use mypy syntax for function annotations

Ethan Furman ethan at stoneleaf.us
Fri Aug 22 03:31:57 CEST 2014


On 08/21/2014 06:24 PM, Antoine Pitrou wrote:
> Le 21/08/2014 20:40, Nick Coghlan a écrit :
>> On 22 Aug 2014 03:27, "Antoine Pitrou"
>> <antoine at python.org
>> <mailto:antoine at python.org>> wrote:
>>  >
>>  >
>>  > Le 21/08/2014 12:15, Nick Coghlan a écrit :
>>  >
>>  >>
>>  >> Given:
>>  >>
>>  >>      Sequence[Number]
>>  >>      Sequence[Number|None]
>>  >>
>>  >> There's still at least three options for extending the syntax even
>> further:
>>  >>
>>  >>      Sequence[Number](EXPR)
>>  >>      Sequence[Number:EXPR]
>>  >>      Sequence[Number,EXPR]
>>  >>
>>  >> You'd probably want to use strings at that point, for the same reasons
>>  >> PyContracts does. For example:
>>  >>
>>  >>      Number,"N>0" # Positive number
>>  >>      Sequence[Number,"N>0"] # Sequence of positive numbers
>>  >>      Sequence[Number],"N>0" # Non-empty sequence
>>  >
>>  >
>>  > All of those are horrible and un-Pythonic, though.
>>  > Python has one of the most powerful and user-friendly function call
>> syntaxes around, why reinvent something clearly inferior and alien?
>>
>> I don't think it's possible to have "Pythonic" design by contract
>
> We're not talking about design by contract, we're talking about type annotations. "A list of X integers" isn't exactly
> an extremely complicated notion. We shouldn't need weird convoluted syntaxes to express it.

I don't know for whom I am arguing and against whom I am arguing, I just want to remind us that a type can be quite a 
bit more complicated than int or str:

   class Counting(int):
       def __new__(cls, value):
           if value < 1:
               raise ValueError('Counting can only be positive')
           ...

--
~Ethan~


More information about the Python-ideas mailing list