[docs] [issue35001] ImportFrom level cannot be optional

thautwarm report at bugs.python.org
Tue Oct 16 14:39:56 EDT 2018


New submission from thautwarm <yaoxiansamma at gmail.com>:

This issue is found from a type hinting problem:

```
class ImportFrom(stmt):	class ImportFrom(stmt):
    module = ...  # type: Optional[_identifier]	    module = ...  # type: Optional[_identifier]
    names = ...  # type: typing.List[alias]	    names = ...  # type: typing.List[alias]
    level = ...  # type: Optional[int]
```

As we can see that `level` here is optional, and it's strange.

I tried `ast.parse` on both Python 3.5/3.6, and found that None of `from a import *`, `from .a import *`, `from ..a import *` and other regular cases result into an ImportFrom AST whose `level` is None.


Then I went to Python-asdl: https://github.com/python/cpython/blob/137b0632dccb992ca11e9445142fb33a29c33a51/Parser/Python.asdl#L44

and got 
   
   ImportFrom(identifier? module, alias* names, int? level)


It seems like a bug. To validate it, I went to https://github.com/python/cpython/blob/97cf0828727ac2a269c89c5aa09570a69a22c83c/Python/ast.c#L3311

and got 

    static stmt_ty
    ast_for_import_stmt(struct compiling *c, const node *n){
       int idx, ndots = 0;
       ...
       return ImportFrom(modname, aliases, ndots, lineno, col_offset, c->c_arena);
       ...
    }

It seems that no reason for `level` being None.

If it's really a bug, IMO it could be also an example that type hinting helps to error detection :-)

----------
assignee: docs at python
components: Documentation
messages: 327840
nosy: docs at python, thautwarm
priority: normal
severity: normal
status: open
title: ImportFrom level cannot be optional
type: enhancement
versions: Python 3.6, Python 3.7, Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35001>
_______________________________________


More information about the docs mailing list