Sitelet https://web.archive.org/web/20260620221844/https://github.com/python/cpython/pull/95773
Skip to content

Fix classvar typing doc#95773

Closed
sp1rs wants to merge 8 commits into
python:mainfrom
sp1rs:fix-classvar-typing-doc
Closed

Fix classvar typing doc#95773
sp1rs wants to merge 8 commits into
python:mainfrom
sp1rs:fix-classvar-typing-doc

Conversation

@sp1rs

@sp1rs sp1rs commented Aug 7, 2022 •

Copy link
Copy Markdown
Contributor

Fix classVar typing doc, fix the example.

sp1rs and others added 3 commits August 15, 2018 01:05
@ghost

ghost commented Aug 7, 2022 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.
CLA signed

@hauntsaninja hauntsaninja left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure this is an improvement since it makes it less obvious the difference between damage and stats. The reason the example isn't runnable or checkable as-is is because we've elided the def __init__(self, damage): self.damage = damage (presumably to keep things short). I'd be more in favour of adding that __init__ to the example than the current change.

@sp1rs

sp1rs commented Aug 7, 2022

Copy link
Copy Markdown
Contributor Author

Not sure this is an improvement since it makes it less obvious the difference between damage and stats. The reason the example isn't runnable as-is is because we've elided the def __init__(self, damage): self.damage = damage (presumably to keep things short). I'd be more in favour of adding that __init__ to the example than the current change.

oh yes, it make sense. Thanks. I will fix the PR.

@TeamSpen210

Copy link
Copy Markdown

It’d probably be a good idea to keep the instance variable annotation outside __init__, so it’s also clear what a lack of ClassVar does.

Comment thread Doc/library/typing.rst Outdated
Comment on lines +993 to +996
stats: ClassVar[dict[str, int]] = {} # class variable
damage: int = 10 # instance variable

def __init__(self, damage: int):
self.damage = damage # instance variable

@hauntsaninja hauntsaninja Aug 7, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should still keep the instance variable type annotation at the class-level:

      class Starship:
          stats: ClassVar[dict[str, int]] = {} # class variable
          damage: int = 10                     # instance variable
          
          def __init__(self, damage: int): ...

(would make a suggestion, but github doesn't let suggestions include deleted lines)

This is important because the confusion that ClassVar resolves is what the intention of a class-level annotation is

@Fidget-Spinner

Copy link
Copy Markdown
Member

Sorry but I agree with @hauntsaninja and I don't think this is an improvement over the current docs. If anything it distracts from the example.

@erlend-aasland erlend-aasland added the pending The issue will be closed if no feedback is provided label Aug 8, 2022
@erlend-aasland

Copy link
Copy Markdown
Contributor

Based on the responses to this PR, I'm adding the pending close label.

@JelleZijlstra

Copy link
Copy Markdown
Member

I think the example is fine as is. Adding an __init__ is only distracting.

@Fidget-Spinner

Fidget-Spinner commented Aug 10, 2022 •

Copy link
Copy Markdown
Member

Two typing maintainers are against this change. I'm closing the PR.

@AA-Turner AA-Turner removed the pending The issue will be closed if no feedback is provided label Apr 6, 2025
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.

8 participants