-
-
Notifications
You must be signed in to change notification settings - Fork 367
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
Use enum for typing NOTHING #983
Conversation
823e4e1
to
440afa3
Compare
Hm, I don't hate it. |
Forgot to mention-- this is the recommended way of doing this, at least until PEP-661 lands. |
I'm not really firm on these topics, so I'm assigning this to Tin. I hope the test_mypy failures are just caused by this? 😅 |
I didn't see any local failures nor failures in CI-- maybe I missed something? One thing I didn't consider is that mypy supports python 2-- perhaps that's what's failing? I don't have any experience with how it handles stub files for python 2. |
Don't worry, it's not failing anymore. That was just mypy improving attrs support and subtly changing an error message. I suspect you haven't seen an error, because your local mypy was slightly older. |
This will allow those extending attrs to type NOTHING as Literal[NOTHING].
b060f3b
to
36d942a
Compare
However, since a type stub file overrides any typing from the python module, the enum has to be (mostly) repeated in the stub. If we're fine with it, that's okay. If not, we can work around this by putting it in its own module, and reimporting it in
|
f1f96f9
to
2358f8e
Compare
@hynek I think this is ready to go. We we want a changelog entry? |
yeah, because it probably breaks someone too ideally with a short summary of the upsides please because i keep forgetting myself |
The only upside is that it allows the use of A project I'm working on does some of its own initialization logic, and inspecting (We have a compatibility shim that just re-exports (Side note: if you want to know why we're recreating so much initialization logic...)It's so we can:
|
Ah, I forgot to revisit this one part:
@frozen
class Foo:
no_default: int = field()
two_default: int = field(default=2) Someone inspecting the
If you want, fixing this could be handled as part of this PR, or another. Like you said, it might break someone's code (but I think it'll mainly just change up the type checking they do). Doing it in this PR would mean they handle it all in one fell swoop. Thoughts? |
Where are you seeing this |
In I just tried changing it, and every mypy test starting failing. Maybe that requires a change to the plugin as well? Might be out of scope for this PR then. |
What are the next steps here? |
We add a short changelog entry and merge this. |
@hynek I've added a small changelog entry, merge at will. |
Thanks @KevinMGranger! |
I'm seeing really weird behavior. This works perfectly fine within # foo.py
from typing import Literal
from attrs import NOTHING
def foo(val: str | Literal[NOTHING]) -> None:
pass $ mypy foo.py
foo.py:4: error: Parameter 1 of Literal[...] is invalid
foo.py:4: error: Variable "attr.NOTHING" is not valid as a type
foo.py:4: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
Found 3 errors in 1 file (checked 1 source file) Anyone else seeing this? I'm trying to debug if this is a mypy issue. Edit: even weirder, I added a method to the def test_type(self, val: str | Literal[NOTHING]):
pass No complaints from mypy! (Edit: weirder and weirder. It doesn't complain within the VS Code mypy integration, but complains on the CLI.) If I can't figure this out soon, you may want to back out this change. Sorry :( |
This reverts commit c860e9d.
pyright doesn't complain, while mypy does. It looks like I misread GVR's suggestion, and missed:
There are two (and a half) options here:
|
I think we can leave it and submit a bug report to Mypy. Unfortunate. |
Does it help if you annotate the alias with |
Re: MyPy option: how broken is this? Is this gonna make me fix all my projects like the AttrsInstance thing in 22.1 or is this something that's only broken if you're trying to use the new feature it's supposed to implement? |
Yeah, I think it's only broken if you try to Literal it, and hopefully Mypy fixes that. |
Summary
This will allow those extending attrs to type
NOTHING
asLiteral[NOTHING]
.This is the recommended way of doing this, at least until PEP-661 lands.
I also just realized that
Attribute
typesdefault
asOptional[_T]
, when it should really beUnion[_T, Literal[NOTHING]]
. This would be a prerequisite for that.Pull Request Check List
Our CI fails if coverage is not 100%.
.pyi
).tests/typing_example.py
.attr/__init__.pyi
, they've also been re-imported inattrs/__init__.pyi
.docs/api.rst
by hand.@attr.s()
have to be added by hand too.versionadded
,versionchanged
, ordeprecated
directives.Find the appropriate next version in our
__init__.py
file..rst
files is written using semantic newlines.changelog.d
.