Fwd: A typing question

Paulo da Silva p_d_a_s_i_l_v_a_ns at nonetnoaddress.pt
Sun Oct 30 12:52:02 EDT 2022


Às 10:26 de 30/10/22, Peter J. Holzer escreveu:
> On 2022-10-29 23:59:44 +0100, Paulo da Silva wrote:
>> Às 22:34 de 29/10/22, dn escreveu:
>>> 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.
> 
> Quotes are a bit ugly, but why are they a problem?
> 
> [...]
> 
>> 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 Foos:
> 
> That seems like a bug to me. What is the «Foos» in «Optional[Foos]»
> referring to?
> 
> If it's the class attribute «Foos» then that's not a type and even if
> its type is inferred that's not the same as «Optional[it's type]», or is
> it?
> 
> If it's referring to the global symbol «Foos» (i.e. the class defined
> later) that hasn't been defined yet, so it shouldn't work (or
> alternatively, if forward references are allowed it should always work).

The problem is exactly this.
Is there anything to do without loosing my script structure and usual 
practice? The forward reference only is needed to the "typing thing".
Even if I declare class "Foos: pass" before, then another error arises - 
something like "already declared" below.




More information about the Python-list mailing list