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

Fix most mypy attr-defined & pyright reportAttributeAccessIssue by explicitly defining attributes on classes #4535

Merged
merged 3 commits into from
Aug 8, 2024

Conversation

Avasam
Copy link
Contributor

@Avasam Avasam commented Aug 1, 2024

Summary of changes

Fix most mypy attr-defined & pyright reportAttributeAccessIssue for #2345 by explicitly defining attributes on classes

I have some ideas on how to address the last attr-defined cause by DistributionMetadatalicense_files DistributionMetadata.license_file, but that's out of scope for this PR.

get_finalized_command, reinitialize_command, get_command_obj and get_command_class still require casting, but that should be fixed in typeshed.

Pull Request Checklist

@@ -153,6 +153,7 @@ def run(self):
self._create_wheel_file(bdist_wheel)
except Exception:
traceback.print_exc()
# TODO: Fix false-positive [attr-defined] in typeshed
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Avasam Avasam force-pushed the Fix-most-mypy-attr-defined branch from 2203891 to b8594db Compare August 1, 2024 02:28
_full_name = "" #: Private API, internal use only.
_links_to_dynamic = False #: Private API, internal use only.
_needs_stub = False #: Private API, internal use only.
_file_name = "" #: Private API, internal use only.
Copy link
Contributor

@abravalheri abravalheri Aug 8, 2024

Choose a reason for hiding this comment

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

There is one very edge-y case where it is useful to not define default values beforehand: to avoid people forgetting to call finalize_options if they modify the extension list like in #4529.

Tricky... In this case I feel like would like to have the cake (static type annotations) but eat it too (have the exception happening if someone forgets to call finalize_options).

Copy link
Contributor

@abravalheri abravalheri Aug 8, 2024

Choose a reason for hiding this comment

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

Maybe wrap in a if TYPE_CHECKING and just use the type annotations instead of assigning a value?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

One could also declare the attribute type without a default value:

>>> class A:
...   foo: str
...   bar: str
...
>>> A().foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'A' object has no attribute 'foo'

If it's an issue with static checkers, then it can be guarded by if TYPE_CHECKING. Either way I'll add a comment that a potential AttributeError is expected and wanted. You can have your cake and eat it too, here 😄

@Avasam Avasam force-pushed the Fix-most-mypy-attr-defined branch from 1008fc0 to a0cf7e1 Compare August 8, 2024 16:58
setuptools/extension.py Outdated Show resolved Hide resolved
@Avasam
Copy link
Contributor Author

Avasam commented Aug 8, 2024

New missing coverage https://github.com/pypa/setuptools/actions/runs/10306778328/job/28530606282?pr=4535#step:5:108

setuptools/tests/init.py (66.7%): Missing lines 12

Is better resolved by jaraco/skeleton#130

@abravalheri abravalheri merged commit 896be01 into pypa:main Aug 8, 2024
19 of 21 checks passed
@abravalheri
Copy link
Contributor

Thank you!

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