dropbox / dropbox/sqlalchemy-stubs

Data type for Float column should be float, not Decimal

Open
#178 5 comments 21 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
583
Forks
96
PR merge metrics
No merged PRs in 30d

Description

Related to: https://github.com/dropbox/sqlalchemy-stubs/issues/131

## About

https://github.com/dropbox/sqlalchemy-stubs/pull/132 fixed https://github.com/dropbox/sqlalchemy-stubs/issues/131, a bug that `sqlalchemy.Numeric` was treated as float, not `decimal.Decimal`.
But it may introduce a new bug, that `sqlalchemy.Float` is also treated as `decimal.Decimal`, not float.

## To reproduce

Assigning a float value to `sqlalchemy.Float` is enough to reproduce this bug.

Below is a sample code to reproduce:

```python
from decimal import Decimal
from sqlalchemy import Column, Float, Numeric, Integer
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class Numbers(Base):
__tablename__ = "numbers"
id_ = Column(Integer, primary_key=True)

c_numeric = Column(Numeric, nullable=False)
c_numeric_as_decimal = Column(Numeric(asdecimal=True), nullable=False)
c_numeric_as_float = Column(Numeric(asdecimal=False), nullable=False)
c_float = Column(Float, nullable=False)

def in_float() -> None:
number = 1.0
numbers = Numbers(c_numeric=number, c_numeric_as_decimal=number, c_numeric_as_float=number, c_float=number)
print(type(numbers.c_numeric), type(numbers.c_numeric_as_decimal), type(numbers.c_numeric_as_float), type(numbers.c_float))

def in_decimal() -> None:
number = Decimal(1.0)
numbers = Numbers(c_numeric=number, c_numeric_as_decimal=number, c_numeric_as_float=number, c_float=number)
print(type(numbers.c_numeric), type(numbers.c_numeric_as_decimal), type(numbers.c_numeric_as_float), type(numbers.c_float))

in_float()
in_decimal()
```

```
$ mypy foo.py
foo.py:21: error: Incompatible type for "c_numeric" of "Numbers" (got "float", expected "Decimal")
foo.py:21: error: Incompatible type for "c_numeric_as_decimal" of "Numbers" (got "float", expected "Decimal")
foo.py:21: error: Incompatible type for "c_numeric_as_float" of "Numbers" (got "float", expected "Decimal")
foo.py:21: error: Incompatible type for "c_float" of "Numbers" (got "float", expected "Decimal")
Found 4 errors in 1 file (checked 1 source file)
```

## Expected result

```
$ mypy foo.py
foo.py:21: error: Incompatible type for "c_numeric" of "Numbers" (got "float", expected "Decimal")
foo.py:21: error: Incompatible type for "c_numeric_as_decimal" of "Numbers" (got "float", expected "Decimal")
foo.py:26: error: Incompatible type for "c_numeric_as_float" of "Numbers" (got "Decimal", expected "float") # this one
foo.py:26: error: Incompatible type for "c_float" of "Numbers" (got "Decimal", expected "float") # this one
Found 4 errors in 1 file (checked 1 source file)
```

## Environment

```
$ (cd sqlalchemy-stubs && git rev-parse HEAD)
55470ceab8149db983411d5c094c9fe16343c58b
$ python -c "import sqlalchemy; print(sqlalchemy.__version__)"
1.3.20
$ python -V
Python 3.8.2
$ mypy -V
mypy 0.790
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.