On Tue, Sep 29, 2020 at 12:19 PM Brett Cannon <brett@python.org> wrote:

On Mon, Sep 28, 2020 at 2:10 PM Guido van Rossum <guido@python.org> wrote:
I just realize there is an ambiguity in PEP 484 regarding the ‘import ... as ...‘ syntax (and the other form).

I’ve always interpreted (and intended) this to mean ‘[from ...] import X as X’ (i.e. a redundant ‘as’ clause) and that is how it’s used in typeshed.

But it seems you are interpreting it as anything that uses an ‘as’ clause (as in Brett’s ‘dabuiltins’ example). And the text can indeed be read that way.

I think the key difference in view between the two interpretations is whether you view the `as` clause as part of the import statement or a convenient/implied assignment that's done post-import.

So if the semantics were `as` clauses that lead to a name lacking a leading underscore were consider exposed, that trips my brain up because now because _some_ forms of import dictate my exporting via a special rule that I have to avoid while other don't:

- `import X`; not exported
- `from X import Y`; not exported
- `from X import Y as Z`; exported; brain goes, "why is this form of import special when it comes to naming conventions and not the other forms?"

The `from X import Y as Y` is an explicit action where I'm opting to inline the exporting of the symbol makes more sense to me since it is not a normal thing to do and I have to actively think of the opt-in. Otherwise I assume people can unwrap the naming to explicitly export and have the tools support that as well:

from X import Y
Z = Y

Right. The key observation is that you wouldn't write "from X import Y as Y", because it's redundant, so we can give it a different meaning (if only for tooling).

So do you think we can just update PEP 484? It currently says
* Modules and variables imported into the stub are not considered
  exported from the stub unless the import uses the ``import ... as
  ...`` form or the equivalent ``from ... import ... as ...`` form.
We could amend this (as we've done in a few other places) by adding the following:
  (*UPDATE:* To clarify, the intention here is that only names using
  the form ``X as X`` will be exported, i.e. the name before and after
  ``as`` must be the same.)
That still doesn't say anything about non-stub modules, but at least it states the rule for stubs more clearly.

Unfortunately we'd have to fix mypy and typeshed, since mypy apparently implemented the wider rule that any use of `as ...` constitutes an export. (I never knew this!)

--Guido van Rossum (python.org/~guido)