Opened 5 years ago

Closed 4 years ago

#17061 closed Cleanup/optimization (fixed)

Factor out "get a module-level object from a dotted path" function

Reported by: Carl Meyer Owned by: Claude Paroz
Component: Core (Other) Version: master
Severity: Normal Keywords:
Cc: james@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

We've got importlib.import_module, but we're missing a higher-level function that would take "x.y.z.foo" (and maybe a fallback default?) and return the actual object "foo" from the "x.y.z" module. Instead we've got the same .rsplit(1) code duplicated everywhere we need to do this.

One implementation we could steal is here: https://github.com/erikrose/django-tidings/blob/master/tidings/utils.py#L144

I was sure there was already a ticket for this, but can't seem to find it, so maybe I was wrong...

Attachments (2)

17061-1.diff (36.4 KB) - added by Claude Paroz 4 years ago.
17061-2.diff (35.8 KB) - added by Claude Paroz 4 years ago.
Updated to current trunk

Download all attachments as: .zip

Change History (9)

comment:1 Changed 5 years ago by James Socol

Cc: james@… added

comment:2 Changed 4 years ago by Claude Paroz

Owner: changed from nobody to Claude Paroz

Changed 4 years ago by Claude Paroz

Attachment: 17061-1.diff added

comment:3 Changed 4 years ago by Claude Paroz

Has patch: set

26 files changed, 133 insertions(+), 273 deletions(-)

Changed 4 years ago by Claude Paroz

Attachment: 17061-2.diff added

Updated to current trunk

comment:4 Changed 4 years ago by Claude Paroz

My last doubt is about the name of the function. I've initially chosen import_module_from_path, but should we name it import_object_from_path orimport_attribute_from_path as the imported name is not the module itself, but an attribute of the module (variable, function, class, ...)?

comment:5 in reply to:  4 Changed 4 years ago by Carl Meyer

Replying to claudep:

My last doubt is about the name of the function. I've initially chosen import_module_from_path, but should we name it import_object_from_path orimport_attribute_from_path as the imported name is not the module itself, but an attribute of the module (variable, function, class, ...)?

How about just import_from_path or import_by_path - since we really don't know what type of thing we are importing.

comment:6 Changed 4 years ago by Claude Paroz

Oh, yes, I like import_by_path.

comment:7 Changed 4 years ago by Claude Paroz <claude@…>

Resolution: fixed
Status: newclosed

In 7c5b244826be636429791a8ca76b2adc678e82e7:

Fixed #17061 -- Factored out importing object from a dotted path

Thanks Carl Meyer for the report.

Note: See TracTickets for help on using tickets.
Back to Top