Fwd: A typing question
Paulo da Silva
p_d_a_s_i_l_v_a_ns at nonetnoaddress.pt
Sat Oct 29 18:59:44 EDT 2022
Às 22:34 de 29/10/22, dn escreveu:
> Out of interest, tested snippet in PyCharm, cf native-mypy. It flags the
> original:
>
> GLOBALS.foos: Optional[Foos]=Foos()
>
> but not the fall-back:
>
> GLOBALS.foos=Foos()
>
>
> Must admit, the first query coming to mind was: why is the typing taking
> place at initialisation-time, rather than within the (class) definition?
> At definition time "foos" has already been typed as None by implication!
>
>
> Solution (below) will not work if the mention of Foos in GLOBALS is a
> forward-reference. Either move GLOBALS to suit, or surround "Foos" with
> quotes.
This is the problem for me. So far, without typing, I used to have some
config and globals classes, mostly to just group definitions an make the
program more readable. A matter of taste and style.
Now, "typing" is breaking this, mostly because of this forward reference
issue.
The funny thing is that if I replace foos by Foos it works because it
gets known by the initial initialization :-) !
________________________
from typing import List, Optional
class GLOBALS:
Foos: Optional[Foos]=None
class Foo:
def __init__(self):
pass
class Foos:
Foos: List[Foo]=[]
# SOME GLOBALS ARE USED HERE
def __init__(self):
pass
GLOBALS.Foos=Foos()
________________________
>
>
> Also, these days (Python version allowing) importing "List" is
> unnecessary. Instead could use "list".
>
>
>
> On 30/10/2022 10.23, Sam Ezeh wrote:
>> Do you want the following?
>>
>> ```
>> from typing import List, Optional
>>
>>
>> class GLOBALS:
>> foos: Optional[Foos] = None
>>
>>
>> class Foo:
>> def __init__(self):
>> pass
>>
>>
>> class Foos:
>> Foos: List[Foo] = []
>>
>> def __init__(self):
>> pass
>>
>>
>> GLOBALS.foos = Foos()
>> ```
>>
>> Kind regards,
>> Sam Ezeh
>>
>> On Sat, 29 Oct 2022 at 22:13, Paulo da Silva <
>> p_d_a_s_i_l_v_a_ns at nonetnoaddress.pt> wrote:
>>
>>> 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.
>>>
>>> --
>>> https://mail.python.org/mailman/listinfo/python-list
>>>
>
More information about the Python-list
mailing list