-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Implement support for returning TypedDict for dataclasses.asdict #8583
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
Open
syastrov
wants to merge
46
commits into
python:master
Choose a base branch
from
syastrov:dataclasses-asdict
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
bb9f051
Implement support for returning TypedDict for dataclasses.asdict
syastrov e2f9f06
Remove redundant test. Fix comment typo.
syastrov 2f6ec2d
Test for cases where dataclasses.asdict is called on non-dataclass in…
syastrov a9779e2
Clean up tests, and test more edge-cases.
syastrov c5d0a15
Remove no-longer-needed type: ignore on CheckerPluginInterface.module…
syastrov 454431d
Make typeddicts non-total.
syastrov d809e8b
Address some of review comments (formatting, docs, code nitpicks, rem…
syastrov 4d195cc
Simplify: Remove _transform_type_args and remove unneeded None check …
syastrov b33798c
Fix unused import
syastrov fe19bc9
Add fine-grained test for dataclasses.asdict.
syastrov 6328a7c
Oops, add forgotten fine-grained dataclasses test. And remove redunda…
syastrov 4694299
Only import the module containing TypedDict fallback if dataclasses i…
syastrov 9c29081
Only enable TypedDict for Python >= 3.8.
syastrov d7df77a
Refactor asdict implementation to use TypeTranslator instead of recur…
syastrov e9a56ba
Made TypedDicts returned by asdict total again.
syastrov 2e5240e
Fixed test after total change.
syastrov 52a1c27
Make code a bit more readable, and a bit more robust.
syastrov 43f174c
Fix typo
syastrov 227ba90
After refactoring to use TypeTranslator, ensure Callable and Type[..]…
syastrov d12c665
Address second review comments.
syastrov 45e72d7
Fix return type
syastrov a03f033
Try to address more review comments and fix flake8
syastrov b4d7e15
Add fine grained deps test to help debug asdict dependencies.
syastrov d96d977
Fix some asdict tests missing tuple dependency
syastrov 441b665
Revert "Fix some asdict tests missing tuple dependency"
syastrov 344ca6a
Don't need dep on typing_extensions
syastrov c8858fa
Checker lookup_fully_qualified_or_none: Don't raise KeyError, return …
syastrov 20d7716
Add dependencies for asdict on the referenced dataclasses and its att…
syastrov 5fec41b
Fix fine-grained no-cache test by adding correct dep on dataclass attrs.
syastrov 5862c16
remove unused imports
syastrov 26b7393
Merge branch 'master' into dataclasses-asdict
syastrov d7e0310
Remove error when passing a "non-dataclass" to asdict to reduce false…
syastrov 080c00c
Fix flake8
syastrov 9e45f8f
Fix asdict tests (require using python version 3.7 minimum).
syastrov 38b466a
Merge branch 'master' into dataclasses-asdict
syastrov 74ebc6f
Fix tests for quoting changes
syastrov aef274a
Merge branch 'master' into dataclasses-asdict
97littleleaf11 79f25db
Merge
97littleleaf11 f54e503
Fix
97littleleaf11 9820cfc
Add fixture for tests
97littleleaf11 9f49cac
Add fixture for tests
97littleleaf11 fcd1ff5
Add fixture for tests
97littleleaf11 9fa6a6b
Merge from master
97littleleaf11 6e1585d
Fix
97littleleaf11 d6f9170
Merge branch 'master' of https://github.com/python/mypy into HEAD
97littleleaf11 e226562
Test for a workaround
97littleleaf11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Oops, add forgotten fine-grained dataclasses test. And remove redunda…
…nt test.
- Loading branch information
commit 6328a7cd6141aeca4b4d6b61549fbec626a5b163
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
-- Test cases for fine-grained incremental checking of dataclasses | ||
syastrov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
-- | ||
-- The comment at the top of fine-grained.test explains how these tests | ||
-- work. | ||
|
||
[case testDataclassesAsdictFineGrained] | ||
[file a.py] | ||
from dataclasses import dataclass | ||
from b import AttributeInOtherModule | ||
|
||
@dataclass | ||
class MyDataclass: | ||
attr: AttributeInOtherModule | ||
|
||
[file b.py] | ||
AttributeInOtherModule = str | ||
[file c.py] | ||
from dataclasses import asdict | ||
from a import MyDataclass | ||
syastrov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
reveal_type(asdict(MyDataclass('John'))) | ||
syastrov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
[file b.py.2] | ||
from typing import List | ||
class MyList(List[int]): | ||
pass | ||
AttributeInOtherModule = MyList | ||
[file c.py.2] | ||
syastrov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from dataclasses import asdict | ||
from a import MyDataclass | ||
from b import MyList | ||
reveal_type(asdict(MyDataclass(MyList()))) | ||
|
||
[out] | ||
c.py:3: note: Revealed type is 'TypedDict({'attr'?: builtins.str})' | ||
== | ||
c.py:4: note: Revealed type is 'TypedDict({'attr'?: builtins.list[builtins.int]})' | ||
[builtins fixtures/tuple.pyi] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add couple fine grained (daemon) test cases. The tricky part is that when type of a dataclass attribute changes, then the call to
asdict()
should be reprocessed (put it in a different module). I am not 100% sure we already have enough extra dependencies added by the plugin to guarantee this, so such tests will be really useful.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added one. It revealed that there was no dependency on the fallback type for TypedDict. I tried to add that. Since it depends on python version which TypedDict fallback to use (I think?), I had to add a dependency on all modules (e.g. typing/typing_extensions/mypy_extensions) to the default plugin. Doesn't seem very nice, but I'm not sure of the right way to do it. Do you have any ideas?
Do you have any ideas for more fine-grained tests to add?