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

Add "most common base" rewriter #311

Merged
merged 9 commits into from
Dec 21, 2023
Merged

Add "most common base" rewriter #311

merged 9 commits into from
Dec 21, 2023

Conversation

chrhansk
Copy link
Contributor

This is an attempted fix for #298: It replaces a union of types by the most common (i.e., most specific) base of all types in the union.

@facebook-github-bot
Copy link

Hi @chrhansk!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! The new rewriter in typing.py looks reasonable, modulo a couple comments, and the test for it in test_typing.py looks good. All the rest of the changes in this PR seems unrelated to the union-rewriter feature, and should be removed from the PR.

The PR should also add documentation for the new rewriter.

monkeytype/typing.py Outdated Show resolved Hide resolved
monkeytype/typing.py Outdated Show resolved Hide resolved
@chrhansk
Copy link
Contributor Author

chrhansk commented Dec 17, 2023

Thanks for the feedback. I added a test case covering multiple inheritance and fixed up the call to the zip function. I also renamed the rewrite to make the intentions more clear.

The difference with respect to mro in fact revolves around multiple inheritance: The mro states that in case a class is derived from two or more bases, the resolution uses the first of these bases. The rewriter does however enable a partial rewrite even in case of multiple inheritance (see the test_multiple_bases_nontrivial test case), which would not be possible by simply using mro(). It is enable by only returning a partial list of bases, which is cut off as soon as any ambiguous base is reached.

@chrhansk chrhansk requested a review from carljm December 18, 2023 16:24
Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

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

Ah, yeah, makes sense that we must not reduce to a common base via multiple inheritance.

A few more comments on the code, and also please run pipenv run black monkeytype to fix up the formatting using the autoformatter.

Thank you!

monkeytype/typing.py Outdated Show resolved Hide resolved
monkeytype/typing.py Outdated Show resolved Hide resolved
monkeytype/typing.py Outdated Show resolved Hide resolved
@chrhansk chrhansk requested a review from carljm December 19, 2023 10:17
@carljm
Copy link
Contributor

carljm commented Dec 19, 2023

Code looks good, thanks for the contribution! Do you feel up to adding some documentation as well? It doesn't need to be extensive, just a mention of the new rewriter and a description of what it does. If not, that's fine, I can add the docs myself, but it may be a couple weeks before I have a chance to do that, and I probably won't merge the PR until docs are ready.

@chrhansk
Copy link
Contributor Author

I added some docstrings to the rewriter itself as well as an entry in the changelog. Is that sufficient?

CHANGES.rst Outdated Show resolved Hide resolved
Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

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

Sorry, I wasn't clear enough! The changelog entry and docstrings look great, but what I actually meant was the public-facing documentation (published at https://monkeytype.readthedocs.io/en/stable/) which is in the docs/ subdirectory. Specifically an entry for the new rewriter should be added to the "Type Rewriters" section at the bottom of https://raw.githubusercontent.com/Instagram/MonkeyType/main/doc/generation.rst

chrhansk and others added 2 commits December 20, 2023 23:06
Co-authored-by: Carl Meyer <carl@oddbird.net>
@chrhansk
Copy link
Contributor Author

OK, done.

Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

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

Thanks, looks great!

CHANGES.rst Outdated Show resolved Hide resolved
doc/generation.rst Outdated Show resolved Hide resolved
@carljm carljm merged commit 12460f8 into Instagram:main Dec 21, 2023
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants