Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upmypy crashes with overloads of functools.singledispatch #8356
Comments
|
Same here: Trying to combine Using Python 3.7 with mypy 0.750 Python code: from datetime import datetime, date
import typing
from typing import Union
from functools import singledispatch
from typing_extensions import Literal
DateInType = Union[date, str]
@typing.overload # <---- THIS IS ***line 50*** of td.py raising mypy internal error
def date2str(t: DateInType, out_format: Literal['str']) -> str: ...
@typing.overload
def date2str(t: DateInType, out_format: Literal['date']) -> date: ...
@singledispatch
def date2str(t, out_format):
''' Converts:
- datetime.date to string (YYYY-MM-DD)
- string (YYYY-MM-DD, YYYY/MM/DD, DD-MM-YYYY, ...) to datetime.date
Second argument = 'out_format' can be 'str' or 'date'.
If specified, returns output in string (YYYY-MM-DD) ['str'] or datetime.date format ['date'].
'''
@date2str.register(date)
def _date2str_date(t, out_format='str'):
''' Input format = datetime.date.
Output format, if string = YYYY-MM-DD'''
if out_format == 'str':
return t.strftime('%Y-%m-%d')
elif isinstance(t, datetime):
return t.date()
else:
return t
@date2str.register(str)
def _date2str_str(t, out_format='date'):
''' Input format = string (YYYY-MM-DD, DD-MM-YYYY, ...)'''
dformats = ('%Y-%m-%d', '%Y/%m/%d', '%d-%m-%Y', '%d/%m/%Y', '%Y%m%d')
for i, dformat in enumerate(dformats, start=1):
try:
d = datetime.strptime(t, dformat).date()
break
except ValueError:
if i == len(dformats):
raise
if out_format == 'date':
return d
else:
return date2str(d, out_format=out_format)
dt = datetime.now()
res = date2str(dt, out_format='date')
print(res, type(res))
assert type(res) == date
res2 = date2str(dt.date(), out_format='str')
print(res2, type(res2))
assert type(res2) == str
res3 = date2str('20061005', out_format='date')
print(res3, type(res3))
assert type(res3) == datemypy traceback:
Am I doing something wrong? |
|
Hello, I am an open source noob and would like to try and tackle this issue. Is that possible? |
|
Yes, of course! Welcome. If you get stuck, feel free to ask questions here. |
|
I couldn't replicate the bug on my machine. Using Python 3.8.2 and using my forked version of mypy (v0.770 I believe), copying @jeanmonet's code yielded the following output:
I also tried replicating the error using your (@hauntsaninja ) code but I admit I didn't really understand what was going on there (what does the lambda mean?) cheers |
|
My repro continues to repro for me (against mypy master). Note it contains the exact command I ran. The output you copied looks like it would be the result of running the program (with the Python interpreter), which is expected to work — this is a bug in mypy, not in Python itself. Remember mypy is a static checker, it doesn't actually run your code. If you still can't get it to repro, try sharing the exact commands you're running here. I assume by lambda you mean the def function named If you need a reference for what overload does (and to understand the code better, since the mypy crash occurs while running some overload checking related code) , I recommend https://mypy.readthedocs.io/en/stable/more_types.html#function-overloading. In general, I've found the mypy docs really helpful. |
|
see #2904 |
I was trying out the advice I gave in #8354, and encountered a mypy crash: