Skip to content

check and fixup for mypy - #123

Merged
altendky merged 8 commits into
python-desert:mainfrom
altendky:check_mypy
Aug 4, 2021
Merged

altendky merged 8 commits into
python-desert:mainfrom
altendky:check_mypy

Conversation

@altendky

Copy link
Copy Markdown
Member

No description provided.

@codecov

codecov Bot commented Jul 30, 2021 •

Copy link
Copy Markdown

Codecov Report

Merging #123 (04656a0) into main (c73bda2) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##              main      #123   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            6         6           
  Lines          446       447    +1     
  Branches        69        69           
=========================================
+ Hits           446       447    +1     
Impacted Files Coverage Δ
src/desert/__init__.py 100.00% <100.00%> (ø)
src/desert/_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 013b307...04656a0. Read the comment docs.

@altendky
altendky marked this pull request as ready for review August 2, 2021 14:59
@altendky
altendky requested review from desert-bot and sveinse August 2, 2021 15:00
Comment thread src/desert/__init__.py Resolved
Comment thread .coveragerc Resolved
Comment thread src/desert/_make.py Outdated
field.name: field for field in fields if not field.name.startswith("_")
}
attributes: t.Dict[
str, t.Union[dataclasses.Field, attr.Attribute, marshmallow.fields.Field]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This field annotation, including both types of field, seems broad. Is that what we want?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Mmm, like it should be turned inside out? A union of two dicts such that the keys must always be either attrs or dataclasses? More like above where it is...

fields: t.Union[t.Tuple[dataclasses.Field, ...], t.Tuple[attr.Attribute, ...]]

or here as...

t.Union[
    t.Dict[str, t.Union[dataclasses.Field, marshmallow.fields.Field]],
    t.Dict[str, t.Union[attr.Attribute, marshmallow.fields.Field]],
]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

That looks better. Though tbh I forget how this works. What's the situation where we might have either an mm field or a dc field?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't know why Marshmallow leaked in there... Anyways, mypy doesn't act the way I expect. Digging into it.

https://mypy-play.net/?mypy=latest&python=3.10&gist=0756f026545af8559782e541c5351d80

import typing

these_or_those: typing.Union[typing.List[int], typing.List[str]] = []

reveal_type([x for x in these_or_those])
main.py:3: error: Incompatible types in assignment (expression has type "List[<nothing>]", variable has type "Union[List[int], List[str]]")
main.py:5: note: Revealed type is "builtins.list[Union[builtins.int*, builtins.str*]]"
Found 1 error in 1 file (checked 1 source file)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I dunno. I was directed to https://mail.python.org/archives/list/typing-sig@python.org/thread/GYVM5KEE6URE6PAH7UTK6324M7GWSFQS/ as maybe being related but it seems like one way or another we will not have this "fixed" (if it is in fact a bug).

But looking again I'm not entirely convinced this line is even needed. We reassign (almost) all of the values below in the for loop (that's probably how marshmallow.fields.Field leaked in). "Almost" because the non-init fields are left untouched. But does that really make sense putting attrs/dataclass fields in the schema anyways? It seems not. So maybe just deleting this line is the "real" response to mypy complaining. Let me know what you think. If this is bad then we should add a test to point it out.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I remember being confused about this before. Deleting it seems like a good experiment at the least.

@altendky altendky left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

thanks for the help here

Comment thread .coveragerc Resolved
Comment thread src/desert/__init__.py Resolved
Comment thread src/desert/_make.py Outdated
field.name: field for field in fields if not field.name.startswith("_")
}
attributes: t.Dict[
str, t.Union[dataclasses.Field, attr.Attribute, marshmallow.fields.Field]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Mmm, like it should be turned inside out? A union of two dicts such that the keys must always be either attrs or dataclasses? More like above where it is...

fields: t.Union[t.Tuple[dataclasses.Field, ...], t.Tuple[attr.Attribute, ...]]

or here as...

t.Union[
    t.Dict[str, t.Union[dataclasses.Field, marshmallow.fields.Field]],
    t.Dict[str, t.Union[attr.Attribute, marshmallow.fields.Field]],
]

@altendky
altendky requested a review from desert-bot August 3, 2021 18:32
@altendky

altendky commented Aug 4, 2021

Copy link
Copy Markdown
Member Author

Alrighty, I think all points have been addressed and it's ready for a final review.

@altendky
altendky merged commit 2596963 into python-desert:main Aug 4, 2021
@altendky
altendky deleted the check_mypy branch August 4, 2021 17:56
@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