Skip to content

Commit

Permalink
Fix clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Mar 21, 2022
1 parent 73a4018 commit 2b72e00
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 38 deletions.
31 changes: 12 additions & 19 deletions bindings/python/src/encoding.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use pyo3::exceptions;
use pyo3::prelude::*;
use pyo3::types::*;
use pyo3::{PyObjectProtocol, PySequenceProtocol};
use tk::tokenizer::{Offsets, PaddingDirection};
use tk::utils::truncation::TruncationDirection;
use tokenizers as tk;
Expand All @@ -21,24 +20,6 @@ impl From<tk::tokenizer::Encoding> for PyEncoding {
}
}

#[pyproto]
impl PyObjectProtocol for PyEncoding {
fn __repr__(&self) -> PyResult<String> {
Ok(format!(
"Encoding(num_tokens={}, attributes=[ids, type_ids, tokens, offsets, \
attention_mask, special_tokens_mask, overflowing])",
self.encoding.get_ids().len()
))
}
}

#[pyproto]
impl PySequenceProtocol for PyEncoding {
fn __len__(&self) -> PyResult<usize> {
Ok(self.encoding.len())
}
}

#[pymethods]
impl PyEncoding {
#[new]
Expand Down Expand Up @@ -73,6 +54,18 @@ impl PyEncoding {
}
}

fn __repr__(&self) -> PyResult<String> {
Ok(format!(
"Encoding(num_tokens={}, attributes=[ids, type_ids, tokens, offsets, \
attention_mask, special_tokens_mask, overflowing])",
self.encoding.get_ids().len()
))
}

fn __len__(&self) -> PyResult<usize> {
Ok(self.encoding.len())
}

/// Merge the list of encodings into one final :class:`~tokenizers.Encoding`
///
/// Args:
Expand Down
4 changes: 0 additions & 4 deletions bindings/python/src/normalizers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::sync::{Arc, RwLock};
use pyo3::exceptions;
use pyo3::prelude::*;
use pyo3::types::*;
use pyo3::PySequenceProtocol;

use crate::error::ToPyResult;
use crate::utils::{PyNormalizedString, PyNormalizedStringRefMut, PyPattern};
Expand Down Expand Up @@ -365,10 +364,7 @@ impl PySequence {
fn __getnewargs__<'p>(&self, py: Python<'p>) -> &'p PyTuple {
PyTuple::new(py, &[PyList::empty(py)])
}
}

#[pyproto]
impl PySequenceProtocol for PySequence {
fn __len__(&self) -> usize {
0
}
Expand Down
7 changes: 2 additions & 5 deletions bindings/python/src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use pyo3::class::basic::CompareOp;
use pyo3::exceptions;
use pyo3::prelude::*;
use pyo3::types::*;
use pyo3::PyObjectProtocol;
use tk::models::bpe::BPE;
use tk::tokenizer::{
Model, PaddingDirection, PaddingParams, PaddingStrategy, PostProcessor, TokenizerImpl,
Expand Down Expand Up @@ -201,10 +200,8 @@ impl PyAddedToken {
fn get_normalized(&self) -> bool {
self.get_token().normalized
}
}
#[pyproto]
impl PyObjectProtocol for PyAddedToken {
fn __str__(&'p self) -> PyResult<&'p str> {

fn __str__(&self) -> PyResult<&str> {
Ok(&self.content)
}

Expand Down
2 changes: 1 addition & 1 deletion bindings/python/src/trainers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ mod tests {
let gil = Python::acquire_gil();
assert_eq!(
"tokenizers.trainers.BpeTrainer",
py_bpe.as_ref(gil.python()).get_type().name()
py_bpe.as_ref(gil.python()).get_type().name().unwrap()
);
}
}
11 changes: 2 additions & 9 deletions bindings/python/src/utils/normalization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::error::ToPyResult;
use pyo3::exceptions;
use pyo3::prelude::*;
use pyo3::types::*;
use pyo3::{PyMappingProtocol, PyObjectProtocol};
use tk::normalizer::{char_to_bytes, NormalizedString, Range, SplitDelimiterBehavior};
use tk::pattern::Pattern;

Expand Down Expand Up @@ -353,10 +352,7 @@ impl PyNormalizedString {
fn replace(&mut self, pattern: PyPattern, content: &str) -> PyResult<()> {
ToPyResult(self.normalized.replace(pattern, content)).into()
}
}

#[pyproto]
impl PyObjectProtocol<'p> for PyNormalizedString {
fn __repr__(&self) -> String {
format!(
r#"NormalizedString(original="{}", normalized="{}")"#,
Expand All @@ -365,14 +361,11 @@ impl PyObjectProtocol<'p> for PyNormalizedString {
)
}

fn __str__(&'p self) -> &'p str {
fn __str__(&self) -> &str {
self.normalized.get()
}
}

#[pyproto]
impl PyMappingProtocol<'p> for PyNormalizedString {
fn __getitem__(&self, range: PyRange<'p>) -> PyResult<Option<PyNormalizedString>> {
fn __getitem__(&self, range: PyRange<'_>) -> PyResult<Option<PyNormalizedString>> {
slice(&self.normalized, &range)
}
}
Expand Down

0 comments on commit 2b72e00

Please sign in to comment.