Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MyPy can't detect default arguments for data types #318

Open
ArneBachmann opened this issue Sep 6, 2017 · 3 comments
Open

MyPy can't detect default arguments for data types #318

ArneBachmann opened this issue Sep 6, 2017 · 3 comments

Comments

@ArneBachmann
Copy link
Contributor

I have this code which compiles and runs fine:

data TimeInterval(fromHour:float, toHour:float, weekFactor:float = 7./7.): pass

Is compiled to Python code:

class TimeInterval(_coconut_NamedTuple("TimeInterval", [("fromHour", 'float'), ("toHour", 'float'), ("weekFactor", 'float')]), _coconut.object):
    __slots__ = ()
    __ne__ = _coconut.object.__ne__
    def __new__(_cls, fromHour, toHour, weekFactor=7. / 7.):
        return _coconut.tuple.__new__(_cls, (fromHour, toHour, weekFactor))
    pass

And it seems that the third argument is still optional.
When I run with --mypy, however, there is an issue raised:

untitled.py:31: error: Missing positional argument "weekFactor" in call to "TimeInterval"
untitled.py:38: error: Too few arguments for "TimeInterval"

where line 31 refers to a function definition providing only the required arguments for the default value:

def b(date, coordinates, window, timeInterval=TimeInterval(fromHour=0., toHour=23.99),  # type: TimeInterval
     obstacles=[], minute_interval=5  # type: int
    ):

and line 38 refers to a manual object creation (testing purposes):

x = TimeInterval(1, 2)

which works fine without --mypy.
Not sure if this is a mypy error or has to do with Coconut. It's hard to report mypy errors with compiled Coconut code, as it's not runnable via python compiled.py and requires lots of Coconut libraries..

@ArneBachmann
Copy link
Contributor Author

untitled.coco.txt

@evhub evhub added the bug label Sep 6, 2017
@evhub evhub changed the title Allow default arguments for data types MyPy can't detect default arguments for data types Sep 6, 2017
@evhub evhub added oversight and removed bug labels Sep 6, 2017
@evhub
Copy link
Owner

evhub commented Sep 6, 2017

@ArneBachmann Yeah, this is a known issue. MyPy can't handle the redefinition of __new__, so it doesn't know that the argument has been made optional. I'll keep this open and maybe try to ping the MyPy people about it.

@iamrecursion
Copy link

If I'm recalling correctly this is an issue specific to the redefinition of __new__ for NamedTuple instances. It's been a while since I've futzed around with MyPy however. It's possibly related to this python/mypy#3307.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants