From 7c5e88f6a64a6e473f2de007bdced3481b3f1adb Mon Sep 17 00:00:00 2001 From: Kristoffer Andersson Date: Fri, 22 Nov 2024 14:53:46 +0100 Subject: [PATCH] feat: bump python-version to 3.9 --- mypy.ini | 2 +- pyproject.toml | 5 ++ ruff.toml | 84 +++++++++---------- .../README.md | 9 ++ .../__init__.py | 2 +- .../annotations.py | 2 +- .../sentiment_analyzer.py | 37 +++----- uv.lock | 49 +++++++++++ 8 files changed, 119 insertions(+), 71 deletions(-) diff --git a/mypy.ini b/mypy.ini index f8584b4..6fcaf90 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1,2 +1,2 @@ [mypy] -python_version = 3.8 +python_version = 3.9 diff --git a/pyproject.toml b/pyproject.toml index 681cbb8..c4a2b80 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,3 +7,8 @@ dependencies = [] [tool.uv.workspace] members = ["sparv-sbx-sentence-sentiment-kb-sent"] + +[dependency-groups] +dev = [ + "mypy>=1.13.0", +] diff --git a/ruff.toml b/ruff.toml index d743b6a..6936748 100644 --- a/ruff.toml +++ b/ruff.toml @@ -1,50 +1,50 @@ -line-length = 97 +line-length = 120 -target-version = "py38" +target-version = "py39" [lint] select = [ - "A", # flake8-builtins - "ANN", # flake8-annotations - "ARG", # flake8-unused-arguments - "B", # flake8-bugbear - "C4", # flake8-comprehensions - "COM", # flake8-commas - "D", # pydocstyle - "D400", # pydocstyle: ends-in-period - "D401", # pydocstyle: non-imperative-mood - "E", # pycodestyle: errors - "F", # Pyflakes - "FLY", # flynt - "FURB", # refurb - "G", # flake8-logging-format - "I", # isort - "ISC", # flake8-implicit-str-concat - "N", # pep8-naming - "PERF", # Perflint - "PIE", # flake8-pie - "PL", # Pylint - # "PT", # flake8-pytest-style - "PTH", # flake8-use-pathlib - "Q", # flake8-quotes - "RET", # flake8-return - "RSE", # flake8-raise - "RUF", # Ruff-specific rules - "SIM", # flake8-simplify - "T20", # flake8-print - "TID", # flake8-tidy-imports - "UP", # pyupgrade - "W", # pycodestyle: warnings + "A", # flake8-builtins + "ANN", # flake8-annotations + "ARG", # flake8-unused-arguments + "B", # flake8-bugbear + "C4", # flake8-comprehensions + "COM", # flake8-commas + "D", # pydocstyle + "D400", # pydocstyle: ends-in-period + "D401", # pydocstyle: non-imperative-mood + "E", # pycodestyle: errors + "F", # Pyflakes + "FLY", # flynt + "FURB", # refurb + "G", # flake8-logging-format + "I", # isort + "ISC", # flake8-implicit-str-concat + "N", # pep8-naming + "PERF", # Perflint + "PIE", # flake8-pie + "PL", # Pylint + # "PT", # flake8-pytest-style + "PTH", # flake8-use-pathlib + "Q", # flake8-quotes + "RET", # flake8-return + "RSE", # flake8-raise + "RUF", # Ruff-specific rules + "SIM", # flake8-simplify + "T20", # flake8-print + "TID", # flake8-tidy-imports + "UP", # pyupgrade + "W", # pycodestyle: warnings ] ignore = [ - "ANN101", # flake8-annotations: missing-type-self (deprecated) - "ANN102", # flake8-annotations: missing-type-cls (deprecated) - "ANN401", # flake8-annotations: any-type - "B008", # flake8-bugbear: function-call-in-default-argument - "ISC001", - "COM812", # flake8-commas: missing-trailing-comma - "PLR09", # Pylint: too-many-* - "SIM105", # flake8-simplify: suppressible-exception + "ANN101", # flake8-annotations: missing-type-self (deprecated) + "ANN102", # flake8-annotations: missing-type-cls (deprecated) + "ANN401", # flake8-annotations: any-type + "B008", # flake8-bugbear: function-call-in-default-argument + "ISC001", + "COM812", # flake8-commas: missing-trailing-comma + "PLR09", # Pylint: too-many-* + "SIM105", # flake8-simplify: suppressible-exception ] preview = true @@ -58,4 +58,4 @@ convention = "google" # Ignore `E402` (import violations) in all `__init__.py` files, and in `path/to/file.py`. [lint.per-file-ignores] -"*/tests/*" = ["D", "ARG002", "E501"] +"**/tests/*" = ["D", "ARG002", "E501", "SIM905"] diff --git a/sparv-sbx-sentence-sentiment-kb-sent/README.md b/sparv-sbx-sentence-sentiment-kb-sent/README.md index e267f82..6e4f773 100644 --- a/sparv-sbx-sentence-sentiment-kb-sent/README.md +++ b/sparv-sbx-sentence-sentiment-kb-sent/README.md @@ -69,6 +69,15 @@ Type | HuggingFace Model | Revision Model | [`KBLab/robust-swedish-sentiment-multiclass`](https://huggingface.co/KBLab/robust-swedish-sentiment-multiclass) | b0ec32dca56aa6182a6955c8f12129bbcbc7fdbd Tokenizer | [`KBLab/megatron-bert-large-swedish-cased-165k`](https://huggingface.co/KBLab/megatron-bert-large-swedish-cased-165k) | 90c57ab49e27b820bd85308a488409dfea25600d +## Minimum Supported Python Version + +This library thrives to support each Python version to End-Of-Life. + +Versions of this library supportes the following Python version: + +- v0.3: Python 3.9 +- v0.2: Python 3.8 + ## Changelog This project keeps a [changelog](./CHANGELOG.md). diff --git a/sparv-sbx-sentence-sentiment-kb-sent/src/sbx_sentence_sentiment_kb_sent/__init__.py b/sparv-sbx-sentence-sentiment-kb-sent/src/sbx_sentence_sentiment_kb_sent/__init__.py index c29a79e..4f0a63e 100644 --- a/sparv-sbx-sentence-sentiment-kb-sent/src/sbx_sentence_sentiment_kb_sent/__init__.py +++ b/sparv-sbx-sentence-sentiment-kb-sent/src/sbx_sentence_sentiment_kb_sent/__init__.py @@ -10,7 +10,7 @@ __description__ = "Annotate sentence with sentiment analysis." __version__ = "0.2.0" -__config__ = [ +__config__: list[sparv_api.Config] = [ sparv_api.Config( f"{PROJECT_NAME}.num_decimals", description="The number of decimals to round the score to", diff --git a/sparv-sbx-sentence-sentiment-kb-sent/src/sbx_sentence_sentiment_kb_sent/annotations.py b/sparv-sbx-sentence-sentiment-kb-sent/src/sbx_sentence_sentiment_kb_sent/annotations.py index 85b3381..5246be1 100644 --- a/sparv-sbx-sentence-sentiment-kb-sent/src/sbx_sentence_sentiment_kb_sent/annotations.py +++ b/sparv-sbx-sentence-sentiment-kb-sent/src/sbx_sentence_sentiment_kb_sent/annotations.py @@ -13,7 +13,7 @@ def annotate_sentence_sentiment( out_sentence_sentiment: sparv_api.Output = sparv_api.Output( f":{PROJECT_NAME}.sentence-sentiment--kb-sent", # cls="sbx_sentence_sentiment_kb_sent", - description="Sentiment analysis of sentence with KBLab/robust-swedish-sentiment-multiclass", # noqa: E501 + description="Sentiment analysis of sentence with KBLab/robust-swedish-sentiment-multiclass", ), word: sparv_api.Annotation = sparv_api.Annotation(""), sentence: sparv_api.Annotation = sparv_api.Annotation(""), diff --git a/sparv-sbx-sentence-sentiment-kb-sent/src/sbx_sentence_sentiment_kb_sent/sentiment_analyzer.py b/sparv-sbx-sentence-sentiment-kb-sent/src/sbx_sentence_sentiment_kb_sent/sentiment_analyzer.py index 2171719..067adfb 100644 --- a/sparv-sbx-sentence-sentiment-kb-sent/src/sbx_sentence_sentiment_kb_sent/sentiment_analyzer.py +++ b/sparv-sbx-sentence-sentiment-kb-sent/src/sbx_sentence_sentiment_kb_sent/sentiment_analyzer.py @@ -1,7 +1,7 @@ """Sentiment analyzer.""" from collections import defaultdict -from typing import Dict, List, Optional, Union +from typing import Optional, Union from sparv import api as sparv_api # type: ignore [import-untyped] from transformers import ( # type: ignore [import-untyped] @@ -45,9 +45,7 @@ def __init__( self.tokenizer = self._default_tokenizer() if tokenizer is None else tokenizer self.model = self._default_model() if model is None else model self.num_decimals = num_decimals - self.classifier = pipeline( - "sentiment-analysis", model=self.model, tokenizer=self.tokenizer - ) + self.classifier = pipeline("sentiment-analysis", model=self.model, tokenizer=self.tokenizer) @classmethod def _default_tokenizer(cls) -> PreTrainedTokenizerFast: @@ -55,9 +53,7 @@ def _default_tokenizer(cls) -> PreTrainedTokenizerFast: @classmethod def _default_model(cls) -> MegatronBertForSequenceClassification: - return AutoModelForSequenceClassification.from_pretrained( - MODEL_NAME, revision=MODEL_REVISION - ) + return AutoModelForSequenceClassification.from_pretrained(MODEL_NAME, revision=MODEL_REVISION) @classmethod def default(cls) -> "SentimentAnalyzer": @@ -70,21 +66,19 @@ def default(cls) -> "SentimentAnalyzer": model = cls._default_model() return cls(model=model, tokenizer=tokenizer) - def analyze_sentence(self, text: List[str]) -> Optional[str]: + def analyze_sentence(self, text: list[str]) -> Optional[str]: """Analyze a sentence. Args: text (Iterable[str]): the text to analyze Returns: - List[Optional[str]]: the sentence annotations. + list[Optional[str]]: the sentence annotations. """ total_length = sum(len(t) for t in text) + len(text) - 1 logger.debug("analyzed text length=%d", total_length) if total_length > MAX_LENGTH: - logger.warning( - "Long sentence (%d chars), splitting and combining results", total_length - ) + logger.warning("Long sentence (%d chars), splitting and combining results", total_length) classifications = self._analyze_in_chunks(text) else: sentence = TOK_SEP.join(text) @@ -94,18 +88,12 @@ def analyze_sentence(self, text: List[str]) -> Optional[str]: collect_label_and_score = ((clss["label"], clss["score"]) for clss in classifications) score_format, score_pred = SCORE_FORMAT_AND_PREDICATE[self.num_decimals] - format_scores = ( - (label, score_format.format(score)) for label, score in collect_label_and_score - ) - filter_out_zero_scores = ( - (label, score) for label, score in format_scores if not score_pred(score) - ) - classification_str = "|".join( - f"{label}:{score}" for label, score in filter_out_zero_scores - ) + format_scores = ((label, score_format.format(score)) for label, score in collect_label_and_score) + filter_out_zero_scores = ((label, score) for label, score in format_scores if not score_pred(score)) + classification_str = "|".join(f"{label}:{score}" for label, score in filter_out_zero_scores) return f"|{classification_str}|" if classification_str else "|" - def _analyze_in_chunks(self, text: List[str]) -> List[Dict[str, Union[str, float]]]: + def _analyze_in_chunks(self, text: list[str]) -> list[dict[str, Union[str, float]]]: classifications_list = [] start_i = 0 curr_length = 0 @@ -121,10 +109,7 @@ def _analyze_in_chunks(self, text: List[str]) -> List[Dict[str, Union[str, float for clss in clsss: classifications_dict[clss["label"]].append(clss["score"]) - return [ - {"label": label, "score": sum(scores) / len(scores)} - for label, scores in classifications_dict.items() - ] + return [{"label": label, "score": sum(scores) / len(scores)} for label, scores in classifications_dict.items()] SCORE_FORMAT_AND_PREDICATE = { diff --git a/uv.lock b/uv.lock index 8280b06..0489aa3 100644 --- a/uv.lock +++ b/uv.lock @@ -598,6 +598,45 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/43/e3/7d92a15f894aa0c9c4b49b8ee9ac9850d6e63b03c9c32c0367a13ae62209/mpmath-1.3.0-py3-none-any.whl", hash = "sha256:a0b2b9fe80bbcd81a6647ff13108738cfb482d481d826cc0e02f5b35e5c88d2c", size = 536198 }, ] +[[package]] +name = "mypy" +version = "1.13.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "mypy-extensions" }, + { name = "tomli", marker = "python_full_version < '3.11'" }, + { name = "typing-extensions" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/e8/21/7e9e523537991d145ab8a0a2fd98548d67646dc2aaaf6091c31ad883e7c1/mypy-1.13.0.tar.gz", hash = "sha256:0291a61b6fbf3e6673e3405cfcc0e7650bebc7939659fdca2702958038bd835e", size = 3152532 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/5e/8c/206de95a27722b5b5a8c85ba3100467bd86299d92a4f71c6b9aa448bfa2f/mypy-1.13.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:6607e0f1dd1fb7f0aca14d936d13fd19eba5e17e1cd2a14f808fa5f8f6d8f60a", size = 11020731 }, + { url = "https://files.pythonhosted.org/packages/ab/bb/b31695a29eea76b1569fd28b4ab141a1adc9842edde080d1e8e1776862c7/mypy-1.13.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:8a21be69bd26fa81b1f80a61ee7ab05b076c674d9b18fb56239d72e21d9f4c80", size = 10184276 }, + { url = "https://files.pythonhosted.org/packages/a5/2d/4a23849729bb27934a0e079c9c1aad912167d875c7b070382a408d459651/mypy-1.13.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:7b2353a44d2179846a096e25691d54d59904559f4232519d420d64da6828a3a7", size = 12587706 }, + { url = "https://files.pythonhosted.org/packages/5c/c3/d318e38ada50255e22e23353a469c791379825240e71b0ad03e76ca07ae6/mypy-1.13.0-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:0730d1c6a2739d4511dc4253f8274cdd140c55c32dfb0a4cf8b7a43f40abfa6f", size = 13105586 }, + { url = "https://files.pythonhosted.org/packages/4a/25/3918bc64952370c3dbdbd8c82c363804678127815febd2925b7273d9482c/mypy-1.13.0-cp310-cp310-win_amd64.whl", hash = "sha256:c5fc54dbb712ff5e5a0fca797e6e0aa25726c7e72c6a5850cfd2adbc1eb0a372", size = 9632318 }, + { url = "https://files.pythonhosted.org/packages/d0/19/de0822609e5b93d02579075248c7aa6ceaddcea92f00bf4ea8e4c22e3598/mypy-1.13.0-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:581665e6f3a8a9078f28d5502f4c334c0c8d802ef55ea0e7276a6e409bc0d82d", size = 10939027 }, + { url = "https://files.pythonhosted.org/packages/c8/71/6950fcc6ca84179137e4cbf7cf41e6b68b4a339a1f5d3e954f8c34e02d66/mypy-1.13.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:3ddb5b9bf82e05cc9a627e84707b528e5c7caaa1c55c69e175abb15a761cec2d", size = 10108699 }, + { url = "https://files.pythonhosted.org/packages/26/50/29d3e7dd166e74dc13d46050b23f7d6d7533acf48f5217663a3719db024e/mypy-1.13.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:20c7ee0bc0d5a9595c46f38beb04201f2620065a93755704e141fcac9f59db2b", size = 12506263 }, + { url = "https://files.pythonhosted.org/packages/3f/1d/676e76f07f7d5ddcd4227af3938a9c9640f293b7d8a44dd4ff41d4db25c1/mypy-1.13.0-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:3790ded76f0b34bc9c8ba4def8f919dd6a46db0f5a6610fb994fe8efdd447f73", size = 12984688 }, + { url = "https://files.pythonhosted.org/packages/9c/03/5a85a30ae5407b1d28fab51bd3e2103e52ad0918d1e68f02a7778669a307/mypy-1.13.0-cp311-cp311-win_amd64.whl", hash = "sha256:51f869f4b6b538229c1d1bcc1dd7d119817206e2bc54e8e374b3dfa202defcca", size = 9626811 }, + { url = "https://files.pythonhosted.org/packages/fb/31/c526a7bd2e5c710ae47717c7a5f53f616db6d9097caf48ad650581e81748/mypy-1.13.0-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:5c7051a3461ae84dfb5dd15eff5094640c61c5f22257c8b766794e6dd85e72d5", size = 11077900 }, + { url = "https://files.pythonhosted.org/packages/83/67/b7419c6b503679d10bd26fc67529bc6a1f7a5f220bbb9f292dc10d33352f/mypy-1.13.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:39bb21c69a5d6342f4ce526e4584bc5c197fd20a60d14a8624d8743fffb9472e", size = 10074818 }, + { url = "https://files.pythonhosted.org/packages/ba/07/37d67048786ae84e6612575e173d713c9a05d0ae495dde1e68d972207d98/mypy-1.13.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:164f28cb9d6367439031f4c81e84d3ccaa1e19232d9d05d37cb0bd880d3f93c2", size = 12589275 }, + { url = "https://files.pythonhosted.org/packages/1f/17/b1018c6bb3e9f1ce3956722b3bf91bff86c1cefccca71cec05eae49d6d41/mypy-1.13.0-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:a4c1bfcdbce96ff5d96fc9b08e3831acb30dc44ab02671eca5953eadad07d6d0", size = 13037783 }, + { url = "https://files.pythonhosted.org/packages/cb/32/cd540755579e54a88099aee0287086d996f5a24281a673f78a0e14dba150/mypy-1.13.0-cp312-cp312-win_amd64.whl", hash = "sha256:a0affb3a79a256b4183ba09811e3577c5163ed06685e4d4b46429a271ba174d2", size = 9726197 }, + { url = "https://files.pythonhosted.org/packages/11/bb/ab4cfdc562cad80418f077d8be9b4491ee4fb257440da951b85cbb0a639e/mypy-1.13.0-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:a7b44178c9760ce1a43f544e595d35ed61ac2c3de306599fa59b38a6048e1aa7", size = 11069721 }, + { url = "https://files.pythonhosted.org/packages/59/3b/a393b1607cb749ea2c621def5ba8c58308ff05e30d9dbdc7c15028bca111/mypy-1.13.0-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:5d5092efb8516d08440e36626f0153b5006d4088c1d663d88bf79625af3d1d62", size = 10063996 }, + { url = "https://files.pythonhosted.org/packages/d1/1f/6b76be289a5a521bb1caedc1f08e76ff17ab59061007f201a8a18cc514d1/mypy-1.13.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:de2904956dac40ced10931ac967ae63c5089bd498542194b436eb097a9f77bc8", size = 12584043 }, + { url = "https://files.pythonhosted.org/packages/a6/83/5a85c9a5976c6f96e3a5a7591aa28b4a6ca3a07e9e5ba0cec090c8b596d6/mypy-1.13.0-cp313-cp313-musllinux_1_1_x86_64.whl", hash = "sha256:7bfd8836970d33c2105562650656b6846149374dc8ed77d98424b40b09340ba7", size = 13036996 }, + { url = "https://files.pythonhosted.org/packages/b4/59/c39a6f752f1f893fccbcf1bdd2aca67c79c842402b5283563d006a67cf76/mypy-1.13.0-cp313-cp313-win_amd64.whl", hash = "sha256:9f73dba9ec77acb86457a8fc04b5239822df0c14a082564737833d2963677dbc", size = 9737709 }, + { url = "https://files.pythonhosted.org/packages/5f/d4/b33ddd40dad230efb317898a2d1c267c04edba73bc5086bf77edeb410fb2/mypy-1.13.0-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:0246bcb1b5de7f08f2826451abd947bf656945209b140d16ed317f65a17dc7dc", size = 11013906 }, + { url = "https://files.pythonhosted.org/packages/f4/e6/f414bca465b44d01cd5f4a82761e15044bedd1bf8025c5af3cc64518fac5/mypy-1.13.0-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:7f5b7deae912cf8b77e990b9280f170381fdfbddf61b4ef80927edd813163732", size = 10180657 }, + { url = "https://files.pythonhosted.org/packages/38/e9/fc3865e417722f98d58409770be01afb961e2c1f99930659ff4ae7ca8b7e/mypy-1.13.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:7029881ec6ffb8bc233a4fa364736789582c738217b133f1b55967115288a2bc", size = 12586394 }, + { url = "https://files.pythonhosted.org/packages/2e/35/f4d8b6d2cb0b3dad63e96caf159419dda023f45a358c6c9ac582ccaee354/mypy-1.13.0-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:3e38b980e5681f28f033f3be86b099a247b13c491f14bb8b1e1e134d23bb599d", size = 13103591 }, + { url = "https://files.pythonhosted.org/packages/22/1d/80594aef135f921dd52e142fa0acd19df197690bd0cde42cea7b88cf5aa2/mypy-1.13.0-cp39-cp39-win_amd64.whl", hash = "sha256:a6789be98a2017c912ae6ccb77ea553bbaf13d27605d2ca20a76dfbced631b24", size = 9634690 }, + { url = "https://files.pythonhosted.org/packages/3b/86/72ce7f57431d87a7ff17d442f521146a6585019eb8f4f31b7c02801f78ad/mypy-1.13.0-py3-none-any.whl", hash = "sha256:9c250883f9fd81d212e0952c92dbfcc96fc237f4b7c92f56ac81fd48460b3e5a", size = 2647043 }, +] + [[package]] name = "mypy-extensions" version = "1.0.0" @@ -1637,6 +1676,16 @@ name = "sparv-sbx-sentiment-analysis-workspace" version = "0.0.0" source = { virtual = "." } +[package.dev-dependencies] +dev = [ + { name = "mypy" }, +] + +[package.metadata] + +[package.metadata.requires-dev] +dev = [{ name = "mypy", specifier = ">=1.13.0" }] + [[package]] name = "stanza" version = "1.5.1"