I think mypy either already supports this or will support very soon (and the same for Optional)

--
Ivan

">

(original) (raw)

On 4 September 2016 at 13:30, Mark Shannon <mark@hotpy.org> wrote:
It would be a real shame if PEP 526 mandates against checkers doing as good as job as possible. Forcing all uses of a variable to have the same type is a major and, IMO crippling, limitation.

E.g.
def foo(x:Optional\[int\])->int:
if x is None:
return -1
return x + 1

If the type of the \*variable\* 'x' is Optional\[int\] then 'return x + 1' doesn't type check. If the type of the \*parameter\* 'x' is Optional\[int\] then a checker can readily verify the above code.

Mark,

First, in addition to the quote from my previous e-mail, I would like to show another quote from PEP 526
"This PEP does not require type checkers to change their type checking rules. It merely provides a more readable syntax to replace type comments"

Second, almost exactly your example has been added to PEP 484:

class Reason(Enum):  
 timeout = 1  
 error = 2

def process(response: Union[str, Reason] = '') -> str:
if response is Reason.timeout:
return 'TIMEOUT'
elif response is Reason.error:
return 'ERROR'
else:
# response can be only str, all other possible values exhausted
return 'PROCESSED: ' + response

I think mypy either already supports this or will support very soon (and the same for Optional)

--
Ivan