Hi!
Consider this simple script ...
___________________
from typing import List, Optional
class GLOBALS:
foos=None
class Foo:
def __init__(self):
pass
class Foos:
Foos: List[Foo]=[]
# SOME GLOBALS ARE USED HERE in a real script
def __init__(self):
pass
GLOBALS.foos: Optional[Foos]=Foos()
___________________
Running mypy on it:
pt9.py:18: error: Type cannot be declared in assignment to non-self
attribute
pt9.py:18: error: Incompatible types in assignment (expression has type "Foos", variable has type "None")
Line 18 is last line and pt9.py is the scrip.
Replacing last line by
GLOBALS.foos=Foos()
and running mypy still gives the second error.
pt9.py:18: error: Incompatible types in assignment (expression has type "Foos", variable has type "None")
What is the common practice in these cases?
Thank you.
On 10/29/2022 1:45 PM, Paulo da Silva wrote:Yes it can.
Hi!
Consider this simple script ...
___________________
from typing import List, Optional
class GLOBALS:
foos=None
class Foo:
def __init__(self):
pass
class Foos:
Foos: List[Foo]=[]
# SOME GLOBALS ARE USED HERE in a real script
def __init__(self):
pass
GLOBALS.foos: Optional[Foos]=Foos()
___________________
Running mypy on it:
pt9.py:18: error: Type cannot be declared in assignment to non-self
attribute
pt9.py:18: error: Incompatible types in assignment (expression has
type "Foos", variable has type "None")
Line 18 is last line and pt9.py is the scrip.
Replacing last line by
GLOBALS.foos=Foos()
and running mypy still gives the second error.
pt9.py:18: error: Incompatible types in assignment (expression has
type "Foos", variable has type "None")
What is the common practice in these cases?
Thank you.
I don't understand
class Foos:
Foos: List[Foo]=[]
If "Foos" is supposed to be a class attribute, then it cannot have the
same name as the class.
I don't understand
class Foos:
Foos: List[Foo]=[]
If "Foos" is supposed to be a class attribute, then it cannot have the same name as the class.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (0 / 16) |
Uptime: | 165:49:20 |
Calls: | 10,385 |
Calls today: | 2 |
Files: | 14,057 |
Messages: | 6,416,528 |