enthought / pyface

pyface: traits-capable windowing framework
Other
107 stars 55 forks source link

Delay imports in api modules if toolkit will be imported #1233

Closed corranwebster closed 1 year ago

corranwebster commented 1 year ago

This is uses module __getattr__ and __dir__ based on PEP 562 which is available now we are dropping Python 3.6 support.

Basic strategy is to handle two classes of imports - those which come directly via the toolkit object, and those which are via an import relative to the api module. This allows us to eventually deprecate and remove stub modules which do nothing more than import using the toolkit, but we don't do that in this PR.

We test by checking that every public attribute returned by dir() can be accessed.

Includes a few drive-by fixes to avoid toolkit selection where where possible. The most significant is adding formal Interface classes for ActionManager and its subclasses (and using those on ApplicationWindow). It also cleans up a deprecated trait on ApplicationWindow.

mdickinson commented 1 year ago

One thought on the general idea: I think I've occasionally used an import pyface.api in the past to deliberately force toolkit selection to occur. (Usually to avoid weird interactions when the toolkit selection happens at a time out of my control.) With this PR, what would be the recommended way to explicitly force toolkit selection?

corranwebster commented 1 year ago

With this PR, what would be the recommended way to explicitly force toolkit selection?

I've added find_toolkit to api.py so I think a reasonable way of doing this would be

from pyface.api import find_toolkit
find_toolkit()

This is idempotent, so the same toolkit object will be found no matter what.