Git Product home page Git Product logo

Comments (3)

oschwald avatar oschwald commented on June 13, 2024 1

The mypy documentation that I linked to provided several different approaches. I don't have any additional suggestions beyond those. For GeoIP2 or GeoLite2 databases, we provide the geoip2 package that has structured model classes for each database type. However, it appears you are using a competitor's database, which I believe has a different internal layout.

from maxmind-db-reader-python.

oschwald avatar oschwald commented on June 13, 2024

As the return type suggests, a get call can return may different types depending on the particular database being used. Please see the mypy documentation on type narrowing.

from maxmind-db-reader-python.

alichaudry avatar alichaudry commented on June 13, 2024

@oschwald I'm familiar w/ type narrowing, but the way I'm getting it to work for this looks incredibly hacky:

def ipinfo_db_lookup(ip_address: str) -> dict | None:
    with maxminddb.open_database(ipinfo_db_path) as reader:
        ip_record = reader.get(ip_address)
        assert isinstance(ip_record, dict | type(None))
        # could also be:
        # assert isinstance(ip_record, dict) or ip_record is None
    return ip_record

Is there a more graceful way of tackling this?

from maxmind-db-reader-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.