Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Following imports #27

Open
LightAndLight opened this issue Apr 9, 2018 · 7 comments
Open

Following imports #27

LightAndLight opened this issue Apr 9, 2018 · 7 comments
Labels

Comments

@LightAndLight
Copy link
Contributor

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
@andreabedini
Copy link

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

@LightAndLight
Copy link
Contributor Author

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

@andreabedini
Copy link

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.

@tonymorris
Copy link
Contributor

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

The + function does not format your disk.

@tonymorris
Copy link
Contributor

maybe, hopefully

@andreabedini
Copy link

>>> 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 ...

@LightAndLight
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants