hpython icon indicating copy to clipboard operation
hpython copied to clipboard

Following imports

Open LightAndLight opened this issue 6 years ago • 7 comments

Eventually I want to be able to parse and validate imported modules. Not going to do it yet, but I'm going to do some brainstorming here.

https://docs.python.org/3.5/reference/import.html#searching

  • We'll have to duplicate the "finders and loaders" logic
  • Should probably make import awareness work for calls to importlib.import_module as well as import statements
  • Need to warn about importing inside control flow, as we can't give useful guarantees

LightAndLight avatar Apr 09 '18 23:04 LightAndLight

I was just thinking about this today. I'd love it if hpython could do some tree-shaking ala webpack. The problem, in python as well as in javascript is that importing modules is not declarative (and can have side effects too!).

Eg. in Python is common to do the following

if FLAG:
  import moduleA
else:
  import moduleB

where FLAG can well be something computed at runtime.

As common as above is this

try:
  import moduleA
except:
  import moduleB

andreabedini avatar Aug 08 '18 11:08 andreabedini

Yep that's the 3rd bullet point :) I've come up against similar issues in scope checking. I wish it wasn't this way.

LightAndLight avatar Aug 08 '18 23:08 LightAndLight

Yep that's the 3rd bullet point :)

😊

I've come up against similar issues in scope checking. I wish it wasn't this way.

Reading https://docs.python.org/3.5/library/functions.html#__import__ is a big depressing.

This function is invoked by the import statement. It can be replaced (by importing the builtins module and assigning to builtins.import) in order to change semantics of the import statement, but doing so is strongly discouraged as it is usually simpler to use import hooks (see PEP 302) to attain the same goals and does not cause issues with code which assumes the default import implementation is in use. Direct use of import() is also discouraged in favor of importlib.import_module().

I wonder what guarantees can be given about Python at all.

andreabedini avatar Aug 09 '18 01:08 andreabedini

I wonder what guarantees can be given about Python at all.

The + function does not format your disk.

tonymorris avatar Aug 09 '18 02:08 tonymorris

maybe, hopefully

tonymorris avatar Aug 09 '18 02:08 tonymorris

>>> int.__add__ = better_add
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't set attributes of built-in/extension type 'int'

At this point I am not sure if I should be happy or sad ...

andreabedini avatar Aug 09 '18 02:08 andreabedini

Definitely happy, because I've tried that same thing before :P

LightAndLight avatar Aug 09 '18 03:08 LightAndLight