Skip to content

more typechecking fixes#4755

Merged
neersighted merged 3 commits into
python-poetry:masterfrom
dimbleby:more-typechecking
Nov 14, 2021
Merged

more typechecking fixes#4755
neersighted merged 3 commits into
python-poetry:masterfrom
dimbleby:more-typechecking

Conversation

@dimbleby
Copy link
Copy Markdown
Contributor

Pull Request Check List

Resolves: #issue-number-here

  • Added tests for changed code.
  • Updated documentation for changed code.

Encouraged by seeing #4751 merged - and so fast, thank you! - I took on a few more of the relatively easy mypy warnings.

I have tried to prefer updating the annotations where possible, but this time I have found reasons to make some modest rearrangements to the code:

  • Config.name seems to be just broken as there is no _file attribute. I find no callers, so have removed it.

  • refactored authentication in uploader.py so that mypy can see that the username and password are not None

  • AIUI poetry is dropping support for python2, so I just went with python3 imports of html.unescape and urllib.parse.quote

  • renamed LegacyRepositoy._get() as LegacyRepository._get_page(): so far as I can see it has little in common with its parent class's PyPiRepository._get(), and mypy complains about them not being the same

One place where I have just updated the annotation to match the code but which smells funny to me is in pool.py. Is it really intended that the repository_name indexing self._lookup might be None? I tried asserting that it was not, but lots of testcases failed...

Copy link
Copy Markdown
Member

@neersighted neersighted left a comment

Choose a reason for hiding this comment

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

Looks mostly good with some questions and some corrections. Needs to be rebased back onto current master which refactored some of the same files and fixed some of the issues addressed here in a better way.

Comment thread poetry/config/config.py
Comment thread poetry/config/config.py
Comment thread poetry/publishing/uploader.py
Comment thread poetry/publishing/uploader.py Outdated
Comment thread poetry/publishing/uploader.py Outdated

return data.asdict()

def _get(self, endpoint: str) -> Optional[Page]:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What was wrong with _get?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

renamed LegacyRepositoy._get() as LegacyRepository._get_page(): so far as I can see it has little in common with its parent class's PyPiRepository._get(), and mypy complains about them not being the same

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the correct solution here is going to be refactoring LegacyRepository to not inherit from PypiRepository. I'd suggest doing that in a follow-up PR, and leaving some # type: and # FIXME annotations here in the meantime.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Even if that's the right long-term fix, get_page() seems to me a sensible name for this method: which gets a Page. Indeed separating this class from the PyPiRepository would leave less reason for their methods to have the same names.

So I'd prefer to leave this change in place: so that we can leave this file mypy-clean - rather than having to whitelist it and allow other errors to slip back in.

(I do not expect to follow up with the change that you propose. Not that I disagree with it; but it's more work than I intend to be taking on here.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think that's sounds logic -- let's go ahead and add a # TODO: annotation above the class as a reminder that this needs to be revisited.

repositories = []

self._lookup: Dict[str, int] = {}
self._lookup: Dict[Optional[str], int] = {}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not sure why there is an Optional here -- None will all hash to the same type in the dict, and I don't think it should ever end up here. I suspect the error is found elsewhere.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

One place where I have just updated the annotation to match the code but which smells funny to me is in pool.py. Is it really intended that the repository_name indexing self._lookup might be None? I tried asserting that it was not, but lots of testcases failed...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm pretty sure this is unintentional behavior (even if it doesn't cause a bug). For now I'd just whitelist it with an annotation, and circle back to address this on a follow-up PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm in two minds about this.

  • The advantage of changing the annotation as I have it is that we can then remove this file from the whitelist, and be confident that further typing errors will not happen.
  • The advantage of your suggestion is that it reminds us that there is a problem here.

Well, why not both? Suggest that we leave the annotation in place for now - it does, after all, reflect what the code is actually doing - but also add a # FIXME saying "surely there's something wrong here".

(Again, I do not expect to follow-up with any fix for that - this looks like quite a pervasive change).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the compromise here is good. Fair enough!

Comment thread poetry/repositories/repository.py
@dimbleby
Copy link
Copy Markdown
Contributor Author

Rebased. I have mostly answered your questions by quoting myself, do say if you'd already seen those comments and still are confused!


return data.asdict()

def _get(self, endpoint: str) -> Optional[Page]:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the correct solution here is going to be refactoring LegacyRepository to not inherit from PypiRepository. I'd suggest doing that in a follow-up PR, and leaving some # type: and # FIXME annotations here in the meantime.

repositories = []

self._lookup: Dict[str, int] = {}
self._lookup: Dict[Optional[str], int] = {}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm pretty sure this is unintentional behavior (even if it doesn't cause a bug). For now I'd just whitelist it with an annotation, and circle back to address this on a follow-up PR.

Comment thread poetry/repositories/repository.py
@dimbleby
Copy link
Copy Markdown
Contributor Author

I've added a single FIXME - reasoning, as above, that

  • _get_page() is a good name regardless of future refactoring
  • accurate annotations are valuable even for inaccurate code

You'll say if you're not persuaded that this is good enough!

Copy link
Copy Markdown
Member

@neersighted neersighted left a comment

Choose a reason for hiding this comment

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

I'd like to leave one more annotation and then we can land this.

The real purpose of mypy checking (in my mind, and many of the other maintainers I suspect) is to force us to consider the correctness of our code and soundness of design. Any PRs to get a file 'mypy clean' will be touching on correctness as well -- like we saw with the (small but excellent) credentials refactor.

I think that leaving some annotations where we've identified soundness issues thanks to mypy will suffice for now -- and hopefully before too long we can revisit those files and refactor them to be more robust.


return data.asdict()

def _get(self, endpoint: str) -> Optional[Page]:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think that's sounds logic -- let's go ahead and add a # TODO: annotation above the class as a reminder that this needs to be revisited.

repositories = []

self._lookup: Dict[str, int] = {}
self._lookup: Dict[Optional[str], int] = {}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the compromise here is good. Fair enough!

should the LegacyRepository really inherit from the PyPiRepository?
@dimbleby
Copy link
Copy Markdown
Contributor Author

Sounds good, thanks for the thoughtful review

@neersighted neersighted merged commit e413a80 into python-poetry:master Nov 14, 2021
@dimbleby dimbleby deleted the more-typechecking branch November 14, 2021 19:06
1nF0rmed pushed a commit to 1nF0rmed/poetry that referenced this pull request Nov 15, 2021
* new FIXME: are nameless repositories desired/expected?

* new TODO: should the LegacyRepository really inherit from the PyPiRepository?
@github-actions
Copy link
Copy Markdown

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Feb 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants