Skip to content

Fix incorrect type hints for timeout and language params in Nominatim and Photon#601

Open
nuglifeleoji wants to merge 1 commit into
geopy:masterfrom
nuglifeleoji:fix/type-hints-timeout-language
Open

Fix incorrect type hints for timeout and language params in Nominatim and Photon#601
nuglifeleoji wants to merge 1 commit into
geopy:masterfrom
nuglifeleoji:fix/type-hints-timeout-language

Conversation

@nuglifeleoji
Copy link
Copy Markdown

Problem (fixes #590, #593, #594)

Static type checkers (Pylance, mypy) report spurious errors for standard usage of geocode() and reverse() in the Nominatim and Photon geocoders.

Issue 1 – timeout parameter (#590)

geolocator.geocode("London", timeout=5)
# Pylance: Argument of type "Literal[5]" cannot be assigned to parameter
#          "timeout" of type "object" in function "geocode"

DEFAULT_SENTINEL is created via type('object', (object,), ...)(), giving it the class name 'object'. Pylance treats this as geopy.geocoders.base.object (not builtins.object), so any integer or float is seen as incompatible.

Issue 2 – language parameter (#593, #594)

geolocator.geocode("London", language="de")
# Pylance: Argument of type "Literal['de']" cannot be assigned to
#          parameter "language" of type "bool" in function "geocode"

language=False causes Pylance to infer the parameter type as bool.

Fix

Add explicit type annotations to geocode() and reverse() in Nominatim and Photon:

  • timeout: Optional[Union[int, float]] = DEFAULT_SENTINEL
  • language: Optional[str] = None (default changed from False to None;
    both are falsy so all if language: checks are unaffected)

The fix uses typing.Optional/typing.Union for Python 3.7 compatibility.

Made with Cursor

… Photon

Static type checkers (Pylance, mypy) reported incorrect type errors when
calling geocode/reverse with standard argument types:

- timeout: Pylance inferred the type as `geopy.geocoders.base.object` (the
  custom sentinel class created via type('object', ...)), making `int`
  values appear incompatible. (geopy#590)
- language: Pylance inferred the type as `bool` from the `False` default,
  making string values like `'de'` appear incompatible. (geopy#593, geopy#594)

Fix both issues in Nominatim and Photon geocoders by:
- Adding `Optional[Union[int, float]]` type annotation for `timeout`
- Adding `Optional[str]` type annotation for `language`
- Changing the `language` default from `False` to `None` (both are falsy
  so `if language:` checks are unaffected, but `None` communicates the
  "not set" intent more clearly to type checkers)

Closes geopy#590, geopy#593, geopy#594

Made-with: Cursor
@Ruiww
Copy link
Copy Markdown

Ruiww commented Apr 2, 2026 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Incorrect type hint for timeout parameter in geolocator.geocode

2 participants