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

Add type annotations #49

Merged
merged 13 commits into from
Feb 8, 2025
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
48 changes: 34 additions & 14 deletions babelfish/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2013 the BabelFish authors. All rights reserved.
# Use of this source code is governed by the 3-clause BSD license
# that can be found in the LICENSE file.
#
import sys

if sys.version_info[0] >= 3:
basestr = str
else:
basestr = basestring
from .converters import (
CountryConverter,
CountryReverseConverter,
LanguageConverter,
LanguageEquivalenceConverter,
LanguageReverseConverter,
)
from .country import COUNTRIES, COUNTRY_MATRIX, Country, country_converters
from .exceptions import CountryConvertError, CountryReverseError, Error, LanguageConvertError, LanguageReverseError
from .language import LANGUAGE_MATRIX, LANGUAGES, Language, language_converters
from .script import SCRIPT_MATRIX, SCRIPTS, Script

from .converters import (LanguageConverter, LanguageReverseConverter, LanguageEquivalenceConverter, CountryConverter,
CountryReverseConverter)
from .country import country_converters, COUNTRIES, COUNTRY_MATRIX, Country
from .exceptions import Error, LanguageConvertError, LanguageReverseError, CountryConvertError, CountryReverseError
from .language import language_converters, LANGUAGES, LANGUAGE_MATRIX, Language
from .script import SCRIPTS, SCRIPT_MATRIX, Script
__all__ = [
'LanguageConverter',
'LanguageReverseConverter',
'LanguageEquivalenceConverter',
'CountryConverter',
'CountryReverseConverter',
'country_converters',
'COUNTRIES',
'COUNTRY_MATRIX',
'Country',
'Error',
'LanguageConvertError',
'LanguageReverseError',
'CountryConvertError',
'CountryReverseError',
'language_converters',
'LANGUAGES',
'LANGUAGE_MATRIX',
'Language',
'SCRIPTS',
'SCRIPT_MATRIX',
'Script',
]
23 changes: 13 additions & 10 deletions babelfish/compat.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2013 the BabelFish authors. All rights reserved.
# Use of this source code is governed by the 3-clause BSD license
# that can be found in the LICENSE file.
#
from __future__ import annotations

from sys import version_info as _python
from typing import IO, Any

if _python >= (3, 9):
# introduced in python 3.9
from importlib.resources import files
else:
from importlib_resources import files
from importlib_resources import files # type: ignore[import-not-found,no-redef]


if _python >= (3, 10):
# .select() was introduced in 3.10
from importlib.metadata import entry_points, EntryPoint as _EntryPoint
from importlib.metadata import EntryPoint as _EntryPoint
from importlib.metadata import EntryPoints, entry_points
else:
from importlib_metadata import entry_points, EntryPoint as _EntryPoint
from importlib_metadata import EntryPoint as _EntryPoint # type: ignore[assignment]
from importlib_metadata import EntryPoints, entry_points # type: ignore[assignment]


def resource_stream(pkg, path):
def resource_stream(pkg: str, path: str) -> IO[bytes]:
return files(pkg).joinpath(f'{path}').open('rb')


def iter_entry_points(group, **kwargs):
def iter_entry_points(group: str, **kwargs: Any) -> EntryPoints:
return entry_points().select(group=group, **kwargs)


class EntryPoint(_EntryPoint):
@staticmethod
def parse(eps):
return EntryPoint(*map(str.strip, eps.split('=')), None)
def parse(eps: str) -> EntryPoint:
return EntryPoint(*map(str.strip, eps.split('=')), None) # type: ignore[call-arg]

def resolve(self):
def resolve(self) -> Any:
return self.load()
Loading