11from collections import OrderedDict
2- from collections .abc import Callable
3- from itertools import islice
2+ from collections .abc import Callable , Iterator
43from typing import TYPE_CHECKING , Any , Union
54
65from django .core .exceptions import ImproperlyConfigured
@@ -652,7 +651,7 @@ def orderable(self) -> bool:
652651 return self ._table .orderable
653652
654653 @property
655- def verbose_name (self ) -> Union [str , SafeString ]:
654+ def verbose_name (self ) -> " Union[str, SafeString]" :
656655 """
657656 Return the verbose name for this column.
658657
@@ -697,12 +696,12 @@ def verbose_name(self) -> Union[str, SafeString]:
697696 return capfirst (name )
698697
699698 @property
700- def visible (self ):
699+ def visible (self ) -> bool :
701700 """Return whether this column is visible."""
702701 return self .column .visible
703702
704703 @property
705- def localize (self ):
704+ def localize (self ) -> Union [ bool , None ] :
706705 """Return `True`, `False` or `None` as described in ``Column.localize``"""
707706 return self .column .localize
708707
@@ -728,7 +727,7 @@ class BoundColumns:
728727 table (`.Table`): the table containing the columns
729728 """
730729
731- def __init__ (self , table , base_columns ):
730+ def __init__ (self , table : "Table" , base_columns ):
732731 self ._table = table
733732 self .columns = OrderedDict ()
734733 for name , column in base_columns .items ():
@@ -740,23 +739,7 @@ def __init__(self, table, base_columns):
740739 )
741740 bound_column .order = getattr (table , "order_" + name , column .order )
742741
743- def iternames (self ):
744- return (name for name , column in self .iteritems ())
745-
746- def names (self ):
747- return list (self .iternames ())
748-
749- def iterall (self ):
750- """
751- Return an iterator that exposes all `.BoundColumn` objects,
752- regardless of visibility or sortability.
753- """
754- return (column for name , column in self .iteritems ())
755-
756- def all (self ):
757- return list (self .iterall ())
758-
759- def iteritems (self ):
742+ def items (self ) -> Iterator [tuple [str , BoundColumn ]]:
760743 """
761744 Return an iterator of ``(name, column)`` pairs (where ``column`` is a `BoundColumn`).
762745
@@ -769,85 +752,63 @@ def iteritems(self):
769752 if name not in self ._table .exclude :
770753 yield (name , self .columns [name ])
771754
772- def items (self ):
773- return list ( self .iteritems ())
755+ def names (self ) -> list [ str ] :
756+ return [ name for name , column in self .items ()]
774757
775- def iterorderable (self ):
776- """
777- Same as `BoundColumns.all` but only returns orderable columns.
758+ def all (self ) -> list [BoundColumn ]:
759+ return [column for name , column in self .items ()]
778760
779- This is useful in templates, where iterating over the full
780- set and checking ``{% if column.ordarable %}`` can be problematic in
781- conjunction with e.g. ``{{ forloop.last }}`` (the last column might not
782- be the actual last that is rendered).
761+ def orderable (self ) -> list [BoundColumn ]:
783762 """
784- return ( x for x in self . iterall () if x . orderable )
763+ Return a list of orderable `.BoundColumn` objects.
785764
786- def itervisible (self ):
765+ This is useful in templates, where iterating over the full set and checking ``{% if column.orderable %}`` can
766+ be problematic in conjunction with e.g. ``{{ forloop.last }}`` (the last column might not be the actual last
767+ that is rendered).
787768 """
788- Same as `.iterorderable` but only returns visible `.BoundColumn` objects.
769+ return [ column for column in self . all () if column . orderable ]
789770
790- This is geared towards table rendering.
791- """
792- return ( x for x in self .iterall () if x .visible )
771+ def visible ( self ) -> list [ BoundColumn ]:
772+ """Return a list of visible `.BoundColumn` objects."""
773+ return [ column for column in self .all () if column .visible ]
793774
794- def hide (self , name ):
795- """
796- Hide a column.
775+ def __iter__ (self ) -> Iterator [BoundColumn ]:
776+ return iter (self .visible ())
797777
798- Arguments:
799- name(str): name of the column
800- """
778+ def hide (self , name : str ) -> None :
779+ """Hide a column by name."""
801780 self .columns [name ].column .visible = False
802781
803- def show (self , name ):
804- """
805- Show a column otherwise hidden.
806-
807- Arguments:
808- name(str): name of the column
809- """
782+ def show (self , name : str ) -> None :
783+ """Show a column otherwise hidden by name."""
810784 self .columns [name ].column .visible = True
811785
812- def __iter__ (self ):
813- """Convenience API, alias of `.itervisible`."""
814- return self .itervisible ()
815-
816- def __contains__ (self , item ):
817- """
818- Check if a column is contained within a `BoundColumns` object.
819-
820- *item* can either be a `~.BoundColumn` object, or the name of a column.
821- """
786+ def __contains__ (self , item : Union [str , BoundColumn ]) -> bool :
787+ """Check if a column is contained within a `BoundColumns` object."""
822788 if isinstance (item , str ):
823- return item in self .iternames ()
824- else :
825- # let's assume we were given a column
826- return item in self . iterall ( )
789+ return item in self .names ()
790+ if isinstance ( item , BoundColumn ) :
791+ return item in self . all ()
792+ return TypeError ( "Argument type must be a string or a BoundColumn." )
827793
828- def __len__ (self ):
794+ def __len__ (self ) -> int :
829795 """Return how many `~.BoundColumn` objects are contained (and visible)."""
830- return len (list ( self .itervisible () ))
796+ return len (self .visible ( ))
831797
832- def __getitem__ (self , index ) :
798+ def __getitem__ (self , index : Union [ int , str ]) -> BoundColumn :
833799 """
834- Retrieve a specific `~.BoundColumn` object.
835-
836- *index* can either be 0-indexed or the name of a column
800+ Retrieve a specific `~.BoundColumn` object by index or name.
837801
838802 .. code-block:: python
839803
840804 columns['speed'] # returns a bound column with name 'speed'
841805 columns[0] # returns the first column
842806 """
843807 if isinstance (index , int ):
844- try :
845- return next (islice (self .iterall (), index , index + 1 ))
846- except StopIteration :
847- raise IndexError
808+ return self .all ()[index ]
848809 elif isinstance (index , str ):
849- for column in self .iterall ():
850- if column . name == index :
810+ for name , column in self .items ():
811+ if name == index :
851812 return column
852813 raise KeyError (
853814 f"Column with name '{ index } ' does not exist; choices are: { self .names ()} "
0 commit comments