0

I have encountered a reference problem like this Example 1;

@dataclass
class Book:
    book_id:int
    book_name:str
    book_library: Library #The object where book is stored


@dataclass
class Library:
    library_id:int
    library_capasity: int
    book_list: list[Book]

In this example shown in above i encountered the Library object is not defined because it is defined after Book class declaration.

To overcome this problem i added a code block like this Example 2;

@dataclass
class Library:
    pass

class Book:
    book_id:int
    book_name:str
    book_library: Library #The object where book is stored


@dataclass
class Library:
    library_id:int
    library_capasity: int
    book_list: list[Book]

After this there were no error.

My questions are listed as below;

  • The method which i used to overcome the problem is forward declaration. Is it a bad code design?
  • Python is an interpreted language and is being interpreted language causes this error which is occurred in Example 1?
  • Can same error in Example 1 might happen in Java or C++ which are compiler based programming languages?
  • 4
    `Book[]` is not valid Python syntax. Also, forward declaration isn't a thing in Python. You're not forward declaring the class; you're defining a second, entirely different class, and annotating `book_library` with the wrong class. – user2357112 Dec 13 '22 at 14:49
  • @user2357112 yes you are right i mean list[Book] – edwar.green Dec 13 '22 at 14:52
  • @user2357112 Is it an entirely a different class but has the same name? Or the second implementation overrides the first one? – edwar.green Dec 13 '22 at 14:53
  • Entirely different class. "Overriding" means exactly that: binding the name `Library` to something besides its original value. – chepner Dec 13 '22 at 14:54

2 Answers2

1

Your question is more of a XY problem, at least in this case.

If you use from __future__ import annotations then there are no errors.

Another work-around is to use string hints: book_library: "Library"

As mentioned book_list: Book[] is not valid, use book_list: list[Book] instead.

Mike Scotty
  • 10,530
  • 5
  • 38
  • 50
0

Proper way to do such typing in Python is using __future__ module:

from __future__ import annotations
from dataclasses import dataclass

@dataclass
class Book:
    book_id:int
    book_name:str
    book_library: Library #The object where book is stored


@dataclass
class Library:
    library_id:int
    library_capasity: int
    book_list: list[Book]

(Also fixed Book[] typing)

matszwecja
  • 6,357
  • 2
  • 10
  • 17
  • That particular future's future is not so certain. At the very least, the string `'Library'` may be used in place of `Library`. (`annotations` simply automates that process.) – chepner Dec 13 '22 at 14:53
  • @chepner I'm not really sure what you mean – matszwecja Dec 13 '22 at 14:57
  • I don't recall the details, but making `annotations` mandatory has been delayed twice, and now there is no set time frame for when, if ever, it will be mandatory. (Whether that means it will remain forever optional, as it is now, or actually be removed at some point, I do not know.) – chepner Dec 13 '22 at 15:02
  • Without the future, `book_library: 'Library'` will work as intended. (It's not *identical* to using an actual class reference, but this is basically what `annotations` does as well: it doesn't evaluate the hint at all, but simply stores it as a `str` in the `__annotations__` attribute.) – chepner Dec 13 '22 at 15:04
  • I'd say that there is a huge difference between it not becoming mandatory and it being removed and the latter seems very unlikely to happen – matszwecja Dec 13 '22 at 15:08
  • We even seem to have a guarantee this will NOT happen in the [docs](https://docs.python.org/3/library/__future__.html): "No feature description will ever be deleted from `__future__`" – matszwecja Dec 13 '22 at 15:09
  • Indeed. I don't think the reasons for keeping it optional are so dire that they would decide to remove it. I would just refer to https://peps.python.org/pep-0563/#python-typing-400 as a starting point for understanding why the `annotations` future has not yet been made mandatory. – chepner Dec 13 '22 at 15:12
  • 1
    Ah, that's a good reference: I retract my half-hearted suggestion that `annotations` could be removed :) – chepner Dec 13 '22 at 15:13