Skip to content

Strange "Attributes without a default cannot follow attributes with one" when extending a dataclass from a package #6960

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

Closed
reinhrst opened this issue Jun 8, 2019 · 7 comments
Labels
bug mypy got something wrong priority-1-normal topic-incremental topic-plugins The plugin API and ideas for new plugins

Comments

@reinhrst
Copy link

reinhrst commented Jun 8, 2019

Apologies upfront, this must be the worst bug report I've ever made. I can only reproduce it every now and then, and only in a quite complicated code base. Just logging it here in case it rings a bell (or someone else has the same issue).

I have a package (which we distribute through our own, private, pypi), with type-support (py.typed file present). There are no stub files, all types are inline.

The package defines a dataclass, e.g. file cars.py

 1. import dataclasses
 2.
 3.
 4.
 5. # some empty lines to illustrate a point
 6.
 7.
 8.
 9.
10. @dataclasses.dataclass(frozen=True)
11. class Car:
12.    name: str
13.    top_speed: float
14.    brand: str = "Renault"

Now in my main program (main.py) I need to get a dataclass that is very much like a Car, I just need to be able to set the top_speed to "uknown" (a string).

 1. import dataclasses
 2. from mypackage import cars
 3.
 4. @dataclasses.dataclass(frozen=True)
 5. class UnknownCar(cars.Car):
 6.    top_speed: str
 7.
 8. mycar = UnknownCar(name="unknown", top_speed="unkown", brand="unknown")

Now I would expect mypy to give a warning (Subclass not compatible with super class). What happens however is this:

The first run, no errors. Subsequent runs (same command) give the errors below (until .mypy-cache/ is removed, after which the first run again is errorfree)

main.py:6 error: Attributes without a default cannot follow attributes with one
main.py:12 error: Attributes without a default cannot follow attributes with one
main.py:13 error: Attributes without a default cannot follow attributes with one

So what seems to happen is that mypy (incorrectly) assumes that the top_speed for UnknownCar will be added as last argument (and then it complains, correctly, that the argument before it, brand, has a default)

The next 2 error however point to the lines where name and top_speed are defined in cars.py (even though the error says main.py). If I add more newlines to the cars.py file, these line numbers in the errormessage change, and if I give default values to them, the errors disappear. By giving a fake default (of dataclasses.field()) to all entries, all errors disappear. Note that no amount of type: ignore in the main.py file seems to be able to get rid of the errors.

The behaviour was observed (however not always consistently) in mypy 0.670, 0.701 and HEAD.

@ilevkivskyi
Copy link
Member

Thanks for reporting!

Hm, interesting, looks like a bug in the caching logic. Could you please try also with --new-semantic-analyzer? (It is available on HEAD.)

@ilevkivskyi ilevkivskyi added bug mypy got something wrong priority-1-normal topic-incremental topic-plugins The plugin API and ideas for new plugins labels Jun 8, 2019
@reinhrst
Copy link
Author

reinhrst commented Jun 9, 2019

Yes, --new-semantic-analyzer leads to the same errors on the second run (before crashing)

(the crash is on a different file, AssertionError: fallback can't be filled out until semanal; would you like this submitted as separate ticket?)

@ilevkivskyi
Copy link
Member

would you like this submitted as separate ticket?

Yes, please open another issue for this.

@reinhrst
Copy link
Author

Yes, please open another issue for this.

Done.

As a side note (may or may not be helpful): I do feel that around the area of code that gives this ttributes without a default cannot follow attributes with one is heavy with circular dependencies, and there were some other issues that felt like they may be due to not all files being processed in the right order by mypy. My first hunch therefore was that somehow the order of reading files changes if the file is in cache -- as I said, this may or may not be useful.

@ilevkivskyi
Copy link
Member

is heavy with circular dependencies

This is why I proposed to try --new-semantic-analyzer, that should be much more robust w.r.t. circular dependencies etc. (at least this is our goal for it). Could you please try it on current master with #6972 merged locally?

@Phlogistique
Copy link
Contributor

With mypy 0.720, which according to http://mypy-lang.org/news.html ships with the new semantic analyzer activated by default, the problem still happens to me.

@reinhrst
Copy link
Author

Just checked this in 0.770, it seems to work now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-1-normal topic-incremental topic-plugins The plugin API and ideas for new plugins
Projects
None yet
Development

No branches or pull requests

3 participants