• A typing question

    From Paulo da Silva@21:1/5 to All on Sat Oct 29 18:45:16 2022
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Passin@21:1/5 to Paulo da Silva on Sat Oct 29 20:14:12 2022
    On 10/29/2022 1:45 PM, Paulo da Silva 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.


    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. Perhaps you meant the class to be named "Foo".
    Then a class attribute of Foos: List[Foo]=[] might barely make sense.
    Even then, remember that each instance of Foo would only know about the
    same (originally empty) list Foos. Is that really what you want?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paulo da Silva@21:1/5 to All on Sun Oct 30 04:10:54 2022
    Às 01:14 de 30/10/22, Thomas Passin escreveu:
    On 10/29/2022 1:45 PM, Paulo da Silva 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.


    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.
    Yes it can.
    You can refer it anywhere by Foos.Foos as a list of Foo elements.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Peter J. Holzer@21:1/5 to Thomas Passin on Sun Oct 30 11:14:20 2022
    On 2022-10-29 20:14:12 -0400, Thomas Passin wrote:
    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.

    Why not? They are in different namespaces.

    #v+
    #!/usr/bin/python3

    class Foos:
    Foos = [1, 2, 3]

    f = Foos()
    print(f.Foos)
    #v-

    This works and I see no reason why it shouldn't work.

    hp

    --
    _ | Peter J. Holzer | Story must make more sense than reality.
    |_|_) | |
    | | | hjp@hjp.at | -- Charles Stross, "Creative writing
    __/ | http://www.hjp.at/ | challenge!"

    -----BEGIN PGP SIGNATURE-----

    iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmNeTnwACgkQ8g5IURL+ KF22Tg/5ATAtWQm0UKz4gn+1vSBdXI+oGbrfSDmPgUMtlSrHMREj/ppDtp7v6Uk9 ksXpzr9MaG5SnyCsJANboEgnnAK6FMXKNPFC+pYueeb3tiZ0z+v61VZ0T8zzQ5qm 0o7FzGol4xx1CfLF9hm24P1fmqjLT8/+Uphqurbf28ViKPO/PcNmOpmYYZSFFRDV eChyNzfdnziVzDEUdlLpwSQy/jQeTY6OWU/IfhRqmgMuB+vMt3CKDdcofmddMSkG egfMjK98hXczAJcrfqAc4ZFsM3KY3mD7uPG2kSO0TKd5S2wvUUejvHxHRdeKZAPQ X4VpxDCPqopmR9Im1xLCk2HYtvuSetprVBppb7kqCMO80uBrUG0zhU9lBIuOv1iH c53bbja9NPZJCJADKeoXxwbgEudqjM2UBBucl3wO26BfpRpo2JB6fqaU1s+X/0Yp VAJFxKIinin2cd69vdUdrhWlB1en18CQGyMlwsqEtYxoEpv3acvolDD4q3AbV4tZ aO62RFvo83vTW+atdVC8HZnGI6xJZEXC186+EHf8Z0+yFc7mgn90QQklfWbrYqLE QnIrgq8zRsOM+i9Gj5FGHFMEduRNBbwvBdgG94hb31fqRdQUhEaFeYt0wLbGvPEf mQDFPtUYINJTw7H/or24iIlna7I+73VtcJu/plG