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

enable and pass mypy --strict #138

Merged
merged 7 commits into from
Aug 18, 2021
Merged

Conversation

altendky
Copy link
Member

No description provided.

@codecov
Copy link

codecov bot commented Aug 11, 2021

Codecov Report

Merging #138 (1db6845) into main (a18ef59) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##              main      #138   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            6         6           
  Lines          447       468   +21     
  Branches        69        69           
=========================================
+ Hits           447       468   +21     
Impacted Files Coverage Δ
src/desert/__init__.py 100.00% <100.00%> (ø)
src/desert/_make.py 100.00% <100.00%> (ø)
tests/test_make.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update adb5951...1db6845. Read the comment docs.

@altendky
Copy link
Member Author

Ugh, that's a lot of stuff to look back at in one big blob...

@altendky altendky requested a review from desert-bot August 11, 2021 16:16
src/desert/__init__.py Show resolved Hide resolved
marshmallow_field: marshmallow.fields.Field,
*,
default: T,
metadata: t.Optional[t.Mapping[object, object]] = None,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are all these default Nones a new decision or is that propagating an old decision? Why can't they be {}?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got itchy about the mutable default. I thought I remembered them being passed down into other function calls, but that's not happening here, so who knows what I was looking at. I'll undo that. At the very least, it doesn't belong in this PR.

src/desert/__init__.py Show resolved Hide resolved
tests/test_make.py Show resolved Hide resolved
@@ -309,18 +322,22 @@ class A:

schema = desert.schema_class(A)()

assert schema.loads('{"x": "1.3"}') == A(decimal.Decimal("1.3"))
assert schema.loads('{"x": "1.3"}') == A(decimal.Decimal("1.3")) # type: ignore[call-arg]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My normal thinking would be to ask for comments explaining ignore directives but maybe it's not worth the bother given we have so many.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I often do add them. I probably was just being lazy here. The issue is that we use module and so the attrs and dataclasses mypy plugins can't tell how to modify the classes we define here. Basically, the handy dynamicness of Python is getting in the way of the static mypy checking. Any ideas how to address this instead of ignoring? I guess maybe we could hint module.dataclass to support a, b, and c anyways. I'll take stab at that. It's a hack, but maybe less silly than the ignores.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a basic issue that mypy that it doesn't handle class decorator syntax outside of plugins (python/mypy#3135). I'm not sure if the change below makes it better since you have to apply the decorator without the decorator syntax, and it's a lie, but this works. Any opinions on this, or other next step ideas? At this point I would stick with the ignores. Maybe a single-point explanation in the DataclassModule class would be sufficient?

diff --git a/tests/test_make.py b/tests/test_make.py
index 8efdcdb..b8c45a0 100644
--- a/tests/test_make.py
+++ b/tests/test_make.py
@@ -18,11 +18,21 @@ import typing_extensions
 import desert
 
 
+@attr.frozen
+class C:
+    # Just a workaround to handle some mypy errors due to not being able to
+    # apply the attrs and dataclass awareness through the DataclassModule
+    # parametrization.  Add any additional attributes that are needed by the
+    # tests.
+    x: object = None
+    y: object = None
+
+
 @attr.frozen(order=False)
 class DataclassModule:
     """Implementation of a dataclass module like attr or dataclasses."""
 
-    dataclass: t.Callable[[type], type]
+    dataclass: t.Callable[[type], t.Type[C]]
     field: t.Callable[..., t.Any] = attr.ib()
     fields: t.Callable[[type], object] = attr.ib()
 
@@ -117,13 +127,14 @@ _assert_dump_load = fixture_from_dict(
 def test_simple(module: DataclassModule) -> None:
     """Load dict into a dataclass instance."""
 
-    @module.dataclass
-    class A:
+    class BareA:
         x: int
 
+    A = module.dataclass(BareA)
+
     data = desert.schema_class(A)().load(data={"x": 5})
 
-    assert data == A(x=5)  # type: ignore[call-arg]
+    assert data == A(x=5)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That workaround may be more complicated than it's worth.

@desert-bot
Copy link
Collaborator

All my comments are pretty trivial so if you'd rather skip them and just merge lemme know.

@altendky altendky requested a review from desert-bot August 18, 2021 15:20
@altendky altendky merged commit 32d4f68 into python-desert:main Aug 18, 2021
@altendky altendky deleted the mypy_strict branch August 18, 2021 17:18
@amin-nejad amin-nejad mentioned this pull request Nov 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants