[issue43037] Importlib reload by module name (String)

2021-01-27 Thread Stefan Mosoi


New submission from Stefan Mosoi :

Weird behaviour (maybe it's my opinion) in reload from importlib

if i do:
import importlib
import sys
import datetime
importlib.reload(datetime.timedelta.__module__)

I get
Typeerror: reload() argument must be a module

but if i do

import importlib
import sys
import datetime
importlib.reload(sys.modules.get(datetime.timedelta.__module__))

it works.

The sys.modules.get i got from reload() code:
def reload(module)
if not module or not isinstance(module, types.ModuleType):
raise TypeError("reload() argument must be a module")
try:
name = module.__spec__.name
except AttributeError:
name = module.__name__

if sys.modules.get(name) is not module:

It wouldn't be easier to check if string do sys.modules.get ?

And as a bonus a reload_module function that gets a class (or anything with 
__module__ and reloads that module, no question asked?) 

And i know it's easy to implement, but it would be nicer if was better handled 
in the lib :)

Notes (if it matters):
Python 3.9.1 x64 Windows
I didn't test on other versions.

--
components: Library (Lib)
messages: 385765
nosy: st3f4n2006
priority: normal
severity: normal
status: open
title: Importlib reload by module name (String)
type: enhancement
versions: Python 3.9

___
Python tracker 
<https://bugs.python.org/issue43037>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43037] Importlib reload by module name (String)

2021-01-27 Thread Stefan Mosoi


Stefan Mosoi  added the comment:

The motivation behind my request is as follow: 
I have a dynamic set class (i don't "know" it) and calling
__module__ for that class return a string, reload requires Module(and i don't 
think __module__ will be changed to module). The other functions (import, 
import_module) recieve a string. 
 
Maybe is trivial to add a one liner, but it's a bit confusing to call another 
function in another module than importlib.

Also (not from what i found when searching) there is no way to get the module 
from name without using import_module/import. 

maybe i found the solution, but there might be others that aren't so lucky.

--

___
Python tracker 
<https://bugs.python.org/issue43037>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43037] Importlib reload by module name (String)

2021-01-27 Thread Stefan Mosoi


Stefan Mosoi  added the comment:

Also 
> without having a reference to module itself

reload() just gets the name of the module

try:
name = module.__spec__.name
except AttributeError:
name = module.__name__

and then 

if sys.modules.get(name) is not module

So it might be just check if is str just skip the tranformation to string.

--

___
Python tracker 
<https://bugs.python.org/issue43037>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43037] Importlib reload by module name (String)

2021-01-27 Thread Stefan Mosoi


Stefan Mosoi  added the comment:

I understand. 
I was using to reload some classes that might have changed/added (updates and 
stuff) without having to reload the hole project. There might be some other 
ways (i found this, and didn't keep researching after).
The documentation didn't warned / informed about real live usage or intentions 
for this to exist(like in REPL).

I will continue using this as i use it now and i fully accept the consequences. 

Thank you for the time and answers.

--

___
Python tracker 
<https://bugs.python.org/issue43037>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com