From f7eb55b26029ea1f7a95c8727d85feb448960598 Mon Sep 17 00:00:00 2001 From: Ethan Smith Date: Tue, 7 Apr 2020 15:57:02 -0700 Subject: [PATCH] Make difflib.SequenceMatcher, filecmp.dircmp, and ... ... fileinput.FileInput generic. --- Lib/difflib.py | 4 ++++ Lib/filecmp.py | 4 ++++ Lib/fileinput.py | 3 +++ Lib/test/test_genericalias.py | 6 ++++++ 4 files changed, 17 insertions(+) diff --git a/Lib/difflib.py b/Lib/difflib.py index 5d756436a67e19..f2215d8d4561c2 100644 --- a/Lib/difflib.py +++ b/Lib/difflib.py @@ -32,6 +32,7 @@ from heapq import nlargest as _nlargest from collections import namedtuple as _namedtuple +from types import GenericAlias Match = _namedtuple('Match', 'a b size') @@ -685,6 +686,9 @@ def real_quick_ratio(self): # shorter sequence return _calculate_ratio(min(la, lb), la + lb) + __class_getitem__ = classmethod(GenericAlias) + + def get_close_matches(word, possibilities, n=3, cutoff=0.6): """Use SequenceMatcher to return list of the best "good enough" matches. diff --git a/Lib/filecmp.py b/Lib/filecmp.py index cfdca1e924f552..7a4da6beb50500 100644 --- a/Lib/filecmp.py +++ b/Lib/filecmp.py @@ -13,6 +13,7 @@ import os import stat from itertools import filterfalse +from types import GenericAlias __all__ = ['clear_cache', 'cmp', 'dircmp', 'cmpfiles', 'DEFAULT_IGNORES'] @@ -247,6 +248,9 @@ def __getattr__(self, attr): self.methodmap[attr](self) return getattr(self, attr) + __class_getitem__ = classmethod(GenericAlias) + + def cmpfiles(a, b, common, shallow=True): """Compare common files in two directories. diff --git a/Lib/fileinput.py b/Lib/fileinput.py index c1b0ec9a8ed084..0c31f93ed8f2e7 100644 --- a/Lib/fileinput.py +++ b/Lib/fileinput.py @@ -73,6 +73,7 @@ """ import sys, os +from types import GenericAlias __all__ = ["input", "close", "nextfile", "filename", "lineno", "filelineno", "fileno", "isfirstline", "isstdin", "FileInput", "hook_compressed", @@ -391,6 +392,8 @@ def isfirstline(self): def isstdin(self): return self._isstdin + __class_getitem__ = classmethod(GenericAlias) + def hook_compressed(filename, mode): ext = os.path.splitext(filename)[1] diff --git a/Lib/test/test_genericalias.py b/Lib/test/test_genericalias.py index 9c5b23e5e5b0fc..ed8120fedcb3dc 100644 --- a/Lib/test/test_genericalias.py +++ b/Lib/test/test_genericalias.py @@ -7,6 +7,9 @@ ) from collections.abc import * from contextlib import AbstractContextManager, AbstractAsyncContextManager +from difflib import SequenceMatcher +from filecmp import dircmp +from fileinput import FileInput from os import DirEntry from re import Pattern, Match from types import GenericAlias, MappingProxyType @@ -21,6 +24,9 @@ class BaseTest(unittest.TestCase): def test_subscriptable(self): for t in (type, tuple, list, dict, set, frozenset, defaultdict, deque, + SequenceMatcher, + dircmp, + FileInput, OrderedDict, Counter, UserDict, UserList, Pattern, Match, AbstractContextManager, AbstractAsyncContextManager,