Skip to content

TableConstraints __eq__ is not comprehensive #2263

@caleb-palmer

Description

@caleb-palmer

The TableConstraints class lists primary_key and foreign_keys arguments as optional. However, the eq method does not account for instances in which primary key and/or foreign_keys is None.

return (
self.primary_key == other.primary_key if other.primary_key else None
) and (self.foreign_keys == other.foreign_keys if other.foreign_keys else None)
.

The eq method should account for instances in which a key for self AND other are None.

Reprex

pk_name = "my_pk"
pk = bigquery.table.PrimaryKey(columns = [pk_name])

fk_name = "my_fk"
column_references = []
table_ref = bigquery.TableReference.from_string("my-project.mydataset.mytable")
fk = bigquery.table.ForeignKey(name = fk_name, referenced_table = table_ref, column_references = column_references)

pk_and_fk_constraint = bigquery.table.TableConstraints(
        primary_key = pk,
        foreign_keys = fk
    )

print(pk_and_fk_constraint == pk_and_fk_constraint) # True

only_pk_constraint = bigquery.table.TableConstraints(
        primary_key = pk,
        foreign_keys = None
    )

print(only_pk_constraint == only_pk_constraint) # None

only_fk_constraint = bigquery.table.TableConstraints(
        primary_key = None,
        foreign_keys = fk
    )

print(only_fk_constraint == only_fk_constraint) # None

empty_constraint = bigquery.table.TableConstraints(
        primary_key = None,
        foreign_keys = None
    )

print(empty_constraint == empty_constraint) # None

I'm unable to find any other eq methods in the package than include conditional logic for None, perhaps this was intended to solve for optional parameters specified as None? If so, the paratheses need to be moved to only evaluate the rhs.

Fix

       return self.primary_key == (other.primary_key if other.primary_key else None) and self.foreign_keys == (other.foreign_keys if other.foreign_keys else None)

Metadata

Metadata

Assignees

Labels

api: bigqueryIssues related to the googleapis/python-bigquery API.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions