Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Columns with nullable=False are typed as optional #198

Open
peteris-zealid opened this issue Jan 12, 2022 · 3 comments
Open

Columns with nullable=False are typed as optional #198

peteris-zealid opened this issue Jan 12, 2022 · 3 comments
Labels
expected behavior that's how it's meant to work. consider the "documentation" label in addition question Further information is requested

Comments

@peteris-zealid
Copy link
Contributor

Describe the bug
Table columns have optional[T] type even when they are defined with nullable=False

Expected behavior
Expected for type to be just "builtins.str" (not optional)

To Reproduce

# Most of this is copy pasted from documentation
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy import select
from sqlalchemy.orm import declarative_base

Base = declarative_base()

class User(Base):
    __tablename__ = 'user'

    id = Column(Integer, primary_key=True)
    name = Column(String, nullable=False)  #  <------------ Added nullable=False here

some_user = User(id=5, name='user')

reveal_type(some_user.name)  # <-------- Revealed type is "Union[builtins.str, None]" 

# Expected for type to be just "builtins.str" (not optional)

Versions.

  • OS:
  • Python: 3.8.9
  • SQLAlchemy: 1.4.27
  • mypy: 0.910
  • SQLAlchemy2-stubs: 0.0.2a19

Have a nice day!
Thank you, you too. You do not see this a lot on the internet. Really appreciate it.

@peteris-zealid peteris-zealid added the requires triage New issue that requires categorization label Jan 12, 2022
@zzzeek zzzeek added expected behavior that's how it's meant to work. consider the "documentation" label in addition question Further information is requested and removed requires triage New issue that requires categorization labels Jan 12, 2022
@zzzeek
Copy link
Member

zzzeek commented Jan 12, 2022

if you are using the mypy plugin, this is expected for the 1.4 series, see https://docs.sqlalchemy.org/en/14/orm/extensions/mypy.html#introspection-of-columns-based-on-typeengine

in 2.0 we will be using a completely different approach for this.

@peteris-zealid
Copy link
Contributor Author

You are correct. Sorry for not reading the manual. You should probably close the issue now.

@zzzeek
Copy link
Member

zzzeek commented Jan 12, 2022

no worries at all , i will likely be switching this behavior for 2.0's approach (which won't use the plugin).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
expected behavior that's how it's meant to work. consider the "documentation" label in addition question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants