setuptools icon indicating copy to clipboard operation
setuptools copied to clipboard

Type `pkg_resources._declare_state` and make it work statically

Open Avasam opened this issue 1 year ago • 3 comments

Summary of changes

After playing around with previous PRs with whether _distribution_finders, _namespace_handlers and _namespace_packages definitions should be moved closer to declarations, I noticed that, since we have to define the variables for other reasons anyway, there's no point to _declare_state's global namespace shenanigans. This PR makes it work for static analysis and, after #4246 is merged, will reduce changes in #4242

Pull Request Checklist

  • [ ] Changes have tests (existing tests should pass, no intended behaviour change)
  • [ ] News fragment added in newsfragments/. (even typing-wise there's no user-facing change here, it's all privates) (See documentation for details)

Avasam avatar Mar 06 '24 02:03 Avasam

@Avasam, I am assuming this change does not affect https://github.com/pypa/setuptools/blob/b4b622e2100f0c2ec739f61e1113a94c40e47466/pkg_resources/init.py#L3320, because https://github.com/pypa/setuptools/blob/b4b622e2100f0c2ec739f61e1113a94c40e47466/pkg_resources/init.py#L3340 is already ensuring working_set is a global variable, right?

abravalheri avatar Mar 06 '24 11:03 abravalheri

@Avasam, I am assuming this change does not affect

https://github.com/pypa/setuptools/blob/b4b622e2100f0c2ec739f61e1113a94c40e47466/pkg_resources/init.py#L3320

, because https://github.com/pypa/setuptools/blob/b4b622e2100f0c2ec739f61e1113a94c40e47466/pkg_resources/init.py#L3340

is already ensuring working_set is a global variable, right?

Yep, I validated for working_set too

(BTW, would it make sense to add the annotations in this PR since it is already introducing the variable declaration?) That would probably showcase the benefits of the approach even more.

I know you've already approved the PR, but I added that change since it's small anyway on already modified lines. After #4246, as part of completely typing the register_* methods, I'll introduce some aliases (same that are already in typeshed) to avoid duplicating those Callable type definition

Avasam avatar Mar 06 '24 17:03 Avasam

This change will also reduce errors in https://github.com/pypa/setuptools/pull/4246 to bring it closer to passing

Avasam avatar Mar 06 '24 19:03 Avasam