[issue40595] AttributeError from type annotation

2020-05-15 Thread Ivan Levkivskyi


Ivan Levkivskyi  added the comment:

Yes, this is as expected. A recommended workaround is to define a type alias 
like `Match = re.Match` before the class body. You can also suppress the 
exception with `from __future__ import annotations` (so that the annotations 
are not evaluated), but static type checkers like mypy will still force you to 
use the alias.

--
nosy: +levkivskyi
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

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



[issue40595] AttributeError from type annotation

2020-05-11 Thread Taylor Robie


Taylor Robie  added the comment:

Ah, I see. If this is intended behavior (which is sounds like it is?) feel free 
to close. Thanks!

--

___
Python tracker 

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



[issue40595] AttributeError from type annotation

2020-05-11 Thread Eric V. Smith


Eric V. Smith  added the comment:

This is not an annotations-only issue. This is no different from:


import re

class MyClass(object):
def re(self):
pass

m = re.Match

Which gives the same error. It's being evaluated at class scope.

--
nosy: +eric.smith

___
Python tracker 

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



[issue40595] AttributeError from type annotation

2020-05-11 Thread Taylor Robie


New submission from Taylor Robie :

Consider the following:
```
import re

class MyClass(object):
def re(self):
pass

def foo(self, m: re.Match):
pass
```

Even though `re` and `MyClass.re` are distinct namespaces, the type annotation 
misses that fact (presumably there is an issue with how it is parsing the AST) 
and attempts to access MyClass.re.Match, resulting in:

`AttributeError: 'function' object has no attribute 'Match'`

Commenting out the definition of `MyClass.re` or reversing the definition order 
resolves the issue.

--
components: Interpreter Core
messages: 368635
nosy: robieta
priority: normal
severity: normal
status: open
title: AttributeError from type annotation
type: behavior
versions: Python 3.7, Python 3.8

___
Python tracker 

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