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

showbase: Add type annotations to ShowBase/ClientRepositoryBase attributes #1217

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Souzooka
Copy link
Contributor

Issue description

Currently, a lot of attributes contained on ShowBase have properties with non-inferable types, and as such these attributes nor their methods/attributes are hinted in IDEs.

Solution description

This MR adds type annotations to most properties defined during instance initialization for ShowBase (and a few for ClientRepositoryBase), so that developers which access these properties can determine easily determine the type of the attribute and the properties of its methods (if type information exists for said type, i.e. panda3d.core types).

A slight caveat is that I did not add typehinting for a few attributes declared in ShowBase, (e.g. ShowBase.texmem) because their types are imported via importlib, and I did not want to break the functionality that is provided via using that import method.
Another caveat is that if the names used in type annotations are removed (e.g. the from panda3d.core import * is changed to import a list of specific names), types imported for annotations (i.e. if they are moved into a if TYPE_CHECKING: block) will need to be modified so that they are a string literal (e.g. Optional["NodePath"]) unless annotations is imported from __future__ (Python 3.7+) or direct is running on Python 3.11+.

I've done my best to verify these type annotations are correct, but please correct me if I have gotten anything wrong.

Checklist

I have done my best to ensure that…

  • …I have familiarized myself with the CONTRIBUTING.md file
  • …this change follows the coding style and design patterns of the codebase
  • …I own the intellectual property rights to this code
  • …the intent of this change is clearly explained
  • …existing uses of the Panda3D API are not broken
  • …the changed code is adequately covered by the test suite, where possible.

@Souzooka Souzooka force-pushed the minor/direct-base-cr-attribute-annotations branch from cc579fe to 8f972cf Compare December 14, 2021 03:18
Copy link
Member

@rdb rdb left a comment

Choose a reason for hiding this comment

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

  • Needs rebase
  • See inline comments
  • Do the annotations all work with Python 3.6?

self.mouse2cam = None
self.buttonThrowers = None
self.mouseWatcher = None
self.pipeList: List[Optional[GraphicsPipe]] = []
Copy link
Member

Choose a reason for hiding this comment

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

Does this need to be Optional? Will there ever be a None added to the list?

self.cTravStack = Stack()
# Ditto for an AppTraverser.
self.appTrav = 0
self.appTrav: Optional[CollisionTraverser] = None
Copy link
Member

Choose a reason for hiding this comment

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

I don't think appTrav is meant to be a CollisionTraverser, but I don't know what it's meant to be - I would suggest leaving this Any for now

@@ -275,7 +278,7 @@ def __init__(self, fStartDirect=True, windowType=None):

#: The global :class:`~panda3d.core.GraphicsEngine`, as returned by
#: GraphicsEngine.getGlobalPtr()
self.graphicsEngine = GraphicsEngine.getGlobalPtr()
self.graphicsEngine: GraphicsEngine = GraphicsEngine.getGlobalPtr()
self.graphics_engine = self.graphicsEngine
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to add an annotation to the underscored version as well?

# Typing annotations
from typing import Optional, TYPE_CHECKING
if TYPE_CHECKING:
from direct.distributed.TimeManager import TimeManager
Copy link
Member

Choose a reason for hiding this comment

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

Not sure about such an optional import - and is it even necessary since you referred to it by string?

self.win = None
self.frameRateMeter = None
self.sceneGraphAnalyzerMeter = None
self.win: Optional[GraphicsWindow] = None
Copy link
Member

Choose a reason for hiding this comment

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

Actually, this may also be a GraphicsBuffer, in offscreen mode. But it should in any case be a GraphicsOutput.

@rdb
Copy link
Member

rdb commented Aug 5, 2023

Ping. Still interested in this?

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.

None yet

2 participants