Opened 4 years ago

Closed 2 years ago

#17061 closed Cleanup/optimization (fixed)

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

Reported by: carljm Owned by: claudep
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 claudep 2 years ago.
17061-2.diff (35.8 KB) - added by claudep 2 years ago.
Updated to current trunk

Download all attachments as: .zip

Change History (9)

comment:1 Changed 4 years ago by jsocol

  • Cc james@… added

comment:2 Changed 2 years ago by claudep

  • Owner changed from nobody to claudep

Changed 2 years ago by claudep

comment:3 Changed 2 years ago by claudep

  • Has patch set

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

Changed 2 years ago by claudep

Updated to current trunk

comment:4 follow-up: Changed 2 years ago by 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, ...)?

comment:5 in reply to: ↑ 4 Changed 2 years ago by carljm

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 2 years ago by claudep

Oh, yes, I like import_by_path.

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

  • Resolution set to fixed
  • Status changed from new to closed

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