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

Use keyword arguments for model classes #189

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ on:
schedule:
- cron: '0 11 * * 2'

permissions: {}

jobs:
CodeQL-Build:

Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ on:
types:
- published

permissions: {}

jobs:
build:
name: Build source distribution
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ on:
schedule:
- cron: '3 15 * * SUN'

permissions: {}

jobs:
build:

Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/zizmor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ on:
pull_request:
branches: ["**"]

permissions: {}

jobs:
zizmor:
name: zizmor latest via PyPI
Expand Down
15 changes: 12 additions & 3 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@
History
-------

4.9.0
++++++++++++++++++

5.0.0
++++++++++++++++++

* BREAKING: The ``raw`` attribute on the model classes has been replaced
with a ``to_dict()`` method. This can be used to get a representation of
the object that is suitable for serialization.
* BREAKING: The ``ip_address`` property on the model classes now always returns
a ``ipaddress.IPv4Address`` or ``ipaddress.IPv6Address``.
* BREAKING: The model and record classes now require all arguments other than
``locales`` and ``ip_address`` to be keyword arguments.
* BREAKING: ``geoip2.mixins`` has been made internal. This normally would not
have been used by external code.
* IMPORTANT: Python 3.9 or greater is required. If you are using an older
version, please use an earlier release.
* ``metro_code`` on ``geoip2.record.Location`` has been deprecated. The
Expand Down
52 changes: 52 additions & 0 deletions geoip2/_internal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""This package contains internal utilities"""

# pylint: disable=too-few-public-methods
from abc import ABCMeta
from typing import Any


class Model(metaclass=ABCMeta):
"""Shared methods for MaxMind model classes"""

def __eq__(self, other: Any) -> bool:
return isinstance(other, self.__class__) and self.to_dict() == other.to_dict()

def __ne__(self, other):
return not self.__eq__(other)

# pylint: disable=too-many-branches
def to_dict(self):
"""Returns a dict of the object suitable for serialization"""
result = {}
for key, value in self.__dict__.items():
if key.startswith("_"):
continue
if hasattr(value, "to_dict") and callable(value.to_dict):
if d := value.to_dict():
result[key] = d
elif isinstance(value, (list, tuple)):
ls = []
for e in value:
if hasattr(e, "to_dict") and callable(e.to_dict):
if e := e.to_dict():
ls.append(e)
elif e is not None:
ls.append(e)
if ls:
result[key] = ls
# We only have dicts of strings currently. Do not bother with
# the general case.
elif isinstance(value, dict):
if value:
result[key] = value
elif value is not None and value is not False:
result[key] = value

# network and ip_address are properties for performance reasons
# pylint: disable=no-member
if hasattr(self, "ip_address") and self.ip_address is not None:
result["ip_address"] = str(self.ip_address)
if hasattr(self, "network") and self.network is not None:
result["network"] = str(self.network)

return result
11 changes: 4 additions & 7 deletions geoip2/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,9 @@ def _model_for(
ip_address: IPAddress,
) -> Union[Country, Enterprise, City]:
(record, prefix_len) = self._get(types, ip_address)
traits = record.setdefault("traits", {})
traits["ip_address"] = ip_address
traits["prefix_len"] = prefix_len
return model_class(record, locales=self._locales)
return model_class(
self._locales, ip_address=ip_address, prefix_len=prefix_len, **record
)

def _flat_model_for(
self,
Expand All @@ -266,9 +265,7 @@ def _flat_model_for(
ip_address: IPAddress,
) -> Union[ConnectionType, ISP, AnonymousIP, Domain, ASN]:
(record, prefix_len) = self._get(types, ip_address)
record["ip_address"] = ip_address
record["prefix_len"] = prefix_len
return model_class(record)
return model_class(ip_address, prefix_len=prefix_len, **record)

def metadata(
self,
Expand Down
15 changes: 0 additions & 15 deletions geoip2/mixins.py

This file was deleted.

Loading
Loading