question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type hints for custom column types using TypeDecorator

See original GitHub issue

First, these stubs are amazing. Thank you!

I am trying to get my custom column types to work like a ArrowType using TypeDecorator from https://docs.sqlalchemy.org/en/latest/core/custom_types.html.

I tried following how you’re using the generic TypeEngine for native types like Column(Boolean), Column(Integer) but am coming up short.

Are custom columns working already? If not i figured i could add it using what you already have for the native types.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
ilevkivskyicommented, Nov 16, 2018

They should work. For example you can write:

class FancyInteger(TypeDecorator[int]):
    ...

There are two caveats:

  • Type decorators are currently only generic in Python type, implementation type is not exposed, if it will be really necessary, we can add a parameter for implementation type (see TODO comment in the stub)
  • The above code works fine with mypy (and probably other type checkers), but not at runtime, so you need a workaround. For example see mypy docs:
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    IntEngine = TypeDecorator[int]
else:
    IntEngine = TypeDecorator

class FancyInteger(IntEngine):
    ...

or similar.

0reactions
NotarySigningAgentcommented, Jun 17, 2020

i’m lost at decorator all yours

On Wed, Jun 17, 2020 at 1:38 AM Michel Albert notifications@github.com wrote:

Sorry for hijacking this old issue, but I think it is related enough to stick this here:

If I have a TypeDecorator which is guaranteed to never store None in the DB, I would like to be able to type-hint this without the use of Optional. Is that possible? Currently I get the following message:

Argument 1 of “process_bind_param” is incompatible with supertype “TypeDecorator”; supertype defines the argument type as “Optional[MyType]”

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/dropbox/sqlalchemy-stubs/issues/36#issuecomment-645208162, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEHEGGBB7TJJELU2NHI5VKDRXBXI5ANCNFSM4GE4F3XA .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom Types - SQLAlchemy 1.4 Documentation
The TypeDecorator allows the creation of custom types which add bind-parameter and result-processing behavior to an existing type object.
Read more >
Custom Types - SQLAlchemy 1.1 Documentation
The TypeDecorator allows the creation of custom types which add bind-parameter and result-processing behavior to an existing type object. It is used when ......
Read more >
python - How to find the column name when using a custom ...
I am trying to define my own column type in sqlalchemy: class NonNegativeInt(TypeDecorator): impl = Integer def process_bind_param(self, ...
Read more >
Custom Types - 《SQLAlchemy 1.3 Documentation》 - 书栈网
The TypeDecorator allows the creation of custom types whichadd bind-parameter and result-processing behavior to an existingtype object. It is ...
Read more >
Different Types of SQLAlchemy with Examples - eduCBA
The SQLAlchemy type is one of the built-in types that can be used for the ... Specifying the sql datatypes used by each...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found