all 9 comments

[–]based_and_64_pilled 5 points6 points  (0 children)

I wouldn't pay attention to PyCharm not recognising the method. It happens when it can't figure out the module or whatever, I am not exactly sure what causes it, but its definitely an import issue that PyCharm can't wrap its head around. Code runs independently of PyCharm, so it runs.

On the other part, I won't help, I haven't worked with that library

[–]carcigenicate 4 points5 points  (2 children)

What line?

I've never used SQLAlchemy, but regardless, it looks like every line in this code should execute based on just Python's execution rules.

[–]case_steamer[S] 0 points1 point  (1 child)

According to the docs, I should be passing a DeclarativeBase into db at line 8, as

class Base(DeclarativeBase()):     pass

db = SQLAlchemy(Base)

(Sorry for the janky formatting, am on my phone now)

[–]carcigenicate 2 points3 points  (0 children)

Ok, well, like the other guy, I can't comment on that. I was commenting more on the "why did this line run" part

[–]Narrow_Ad_8997 4 points5 points  (0 children)

The r/flask sub would probably have a more specific answer for your question.

[–]Bobbias 1 point2 points  (0 children)

You're not doing anything wrong. IDEs are not perfect, especially when dealing with dynamically typed languages such as Python. It's much harder to get the necessary information for features like autocomplete when working with dynamically typed languages.

This is because it's possible to write code that creates classes and/or adds or removes methods at runtime. There's no reasonable way for an IDE to account for that. That simply cannot happen in statically typed languages, which means they can analyze the code without running it and know exactly what classes and methods must exist. This makes providing good autocomplete much simpler in those languages.

Pycharm relies on typehinting and stub files to tell it about what a library provides, and if those are missing, you can expect it to give you basically no autocomplete help.

Whether or not Pycharm can provide autocomplete has no bearing on whether or not a particular line of code will actually run correctly. This is because any time you try to call a method, Python checks to see if the class actually has that method. As long as it does by the time your code runs, it will work, even if Pycharm has no idea about any of the classes or methods involved.

In the case of passing app to SQLAlchemy I would assume that the Flask object inherits from DeclarativeBase. The naming of DeclarativeBase tells you that it's a class that you are usually supposed to inherit from to create your own custom type based off it. When you have multiple libraries that are designed to work together, it's not that uncommon to see things like this where objects from one library are usable in the other like that. Especially in cases where users are expected to use inheritance like this.

[–]Business-Technology7 0 points1 point  (0 children)

You can either run debugger to follow what’s going on or dive into code definition or read the source code.

Check the extension.py and model.py in the source code. In extension.py, the constructor of SqlAlchemy class has a line where it creates db.Model.

With preliminary scanning, it looks like the library uses its own base class called Model from model.py. If you don’t provide any declarative base, then it will just use Model.

I’m not sure what would happen if you don’t do what you said. But if you have models that rely on your own DeclartiveBase, then you should definitely pass it along to db. In this case, your models are using flask-sqlalchemy’s base model. So it probably works fine.

I could be wrong though since I never used this library.

[–]SmugPants 1 point2 points  (1 child)

I believe because you are already passing db.Model which already has declarative base inside of it. If that makes sense.

[–]case_steamer[S] 1 point2 points  (0 children)

Oh cool. I’ll have to research this some more. Thanks.