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

Linting action and pre-commit #72

Merged
merged 4 commits into from
Aug 16, 2022
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
28 changes: 28 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Linting
on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: Check out repository code
uses: actions/checkout@v2

- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: "3.9.13"

- name: flake8 Lint
uses: py-actions/flake8@v2
with:
ignore: "W503,E501,F811,F541,W605,E203,F405,E722,W191,F403,F821,E101,N806,N802,N801,N811,N818"
exclude: "nibiru/proto/*"
max-line-length: "88"
31 changes: 5 additions & 26 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,24 @@ repos:
rev: v3.1.0
hooks:
- id: end-of-file-fixer
exclude: >
(?x)^(
.+\.svg|
.+\.min.js|
.+\.xsd|
.+\.md|
.+\.tsx|
.+\.ts|
.+\.xsd|
.+\.html|
.+\.txt|
.+\.ini|
)$
exclude: nibiru/proto/.+

- id: trailing-whitespace
exclude: >
(?x)^(
.+\.svg|
.+\.min.js|
.+\.xsd|
.+\.md|
.+\.tsx|
.+\.ts|
.+\.xsd|
.+\.html|
.+\.txt|
.+\.ini|
)$
exclude: nibiru/proto/.+

- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
files: \.py$
exclude: nibiru/proto/.+

- repo: https://github.com/PyCQA/isort
rev: 5.10.1
hooks:
- id: isort
files: \.py$
exclude: nibiru/proto/.+
args: ["--profile", "black"]

- repo: https://github.com/pycqa/flake8
Expand Down
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Upgrade `pip` to the latest version, if you see these warnings:
```
WARNING: Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
WARNING: Additional context: user = True home = None root = None prefix = None
```
```
-->

---
Expand All @@ -104,7 +104,7 @@ Upgrade `pip` to the latest version, if you see these warnings:

Our recommended setup is `pyenv` in combination with `poetry`.
- `pyenv` is a tool for installing and managing Python interpreters. This will let you seamlessly switch between Python versions.
- `poetry` is used for managing virtual environments, dependency resolution, package installations, package building, and package publishing.
- `poetry` is used for managing virtual environments, dependency resolution, package installations, package building, and package publishing.
- We assume you're on a Unix machine such as WSL2 Ubuntu, MacOS, or a common Linux distro.

Currently, `nibiru-py` is created with Python 3.9.13. It may be compatible with higher versions, but we only run end-to-end tests in 3.9.13.
Expand Down Expand Up @@ -156,24 +156,24 @@ pyenv global 3.10.4 # switches the global interpreter to 3.10.4

You can verify this works as expected using `python --version`. You may be familiar with using `python3` as the command instead of `python`. With `pyenv`, this is not necessary.

Additional usage and installation instructions can be found in the [pyenv repo](https://github.com/pyenv/pyenv).
Additional usage and installation instructions can be found in the [pyenv repo](https://github.com/pyenv/pyenv).

## Installing `poetry` for dependency resolution and publishing packages

Reference: [Poetry docs](https://python-poetry.org/docs/)

Poetry can be installed with both `curl` and `pip`. We recommended using `curl` so that it will be global to your machine.
Poetry can be installed with both `curl` and `pip`. We recommended using `curl` so that it will be global to your machine.

NOTE We highly, highly, highly recommend that you DO NOT use `brew` to install `poetry`.
NOTE We highly, highly, highly recommend that you DO NOT use `brew` to install `poetry`.
If you use `brew`, it's going to install directly to your system, which prevents you from being able to leverage `pyenv` to seamlessly switch between Python interpreters.

```sh
```sh
# installation with pip: recommended option in tandem with pyenv
pip install poetry
```

```sh
# For UNIX systems - installation with curl
# For UNIX systems - installation with curl
curl -sSL https://install.python-poetry.org/ | python -
```

Expand Down Expand Up @@ -206,7 +206,7 @@ The variables used in the CI build can be found in the `env` section of the [`py
```yaml
jobs:
tests:
env:
env:
# https://www.notion.so/nibiru/Resources-and-Repo-Configs-b31aa8074a2b419d80b0c946ed5efab0
CHAIN_ID: ${{ secrets.CHAIN_ID }}
HOST: ${{ secrets.HOST }}
Expand Down Expand Up @@ -270,7 +270,7 @@ To run shell scripts and commands in the `Makefile`, you'll need to install the
sudo dnf install python3-devel autoconf automake gcc gcc-c++ libffi-devel libtool make pkgconfig
```

### Generating types wth protobuf
### Generating types wth protobuf

The objective is to run `make proto-gen`, which simply executes `scripts/protocgen.sh`.

Expand All @@ -280,12 +280,12 @@ python -m pip install --user grpcio-tools
pip install mypy-protobuf
```

If you get a permissions error such as
If you get a permissions error such as
```
rm: cannot remove 'proto/proto/epochs/query.proto': Permission denied
```
call `sudo chown -R [USER-NAME] proto` using the name of user directory.
You can find the value for `[USER-NAME]` quickly by running `whoami`. In other words, this should work:
call `sudo chown -R [USER-NAME] proto` using the name of user directory.
You can find the value for `[USER-NAME]` quickly by running `whoami`. In other words, this should work:

```sh
sudo chown -R $(whoami) proto
Expand All @@ -303,7 +303,7 @@ poetry build # equivalently, you can run `python -m build`
Enable git hook which will perform linting before each commit:

```shell
pre-commit install
poetry run pre-commit install
```

This will keep your code clean.
Expand Down
7 changes: 6 additions & 1 deletion examples/chain_client/dex/create_pool_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ def main() -> None:
denom=network.fee_denom,
)
]
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo('').with_timeout_height(client.timeout_height)
tx = (
tx.with_gas(gas_limit)
.with_fee(fee)
.with_memo('')
.with_timeout_height(client.timeout_height)
)
tx_raw_bytes = tx.get_signed_tx_data()

# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
Expand Down
7 changes: 6 additions & 1 deletion examples/chain_client/msg_send_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ def main() -> None:
denom=network.fee_denom,
)
]
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo("").with_timeout_height(client.timeout_height)
tx = (
tx.with_gas(gas_limit)
.with_fee(fee)
.with_memo("")
.with_timeout_height(client.timeout_height)
)
tx_raw_bytes = tx.get_signed_tx_data()

# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
Expand Down
7 changes: 6 additions & 1 deletion examples/chain_client/perp/open_position_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ def main() -> None:
denom=network.fee_denom,
)
]
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo('').with_timeout_height(client.timeout_height)
tx = (
tx.with_gas(gas_limit)
.with_fee(fee)
.with_memo('')
.with_timeout_height(client.timeout_height)
)
tx_raw_bytes = tx.get_signed_tx_data()

# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
Expand Down
38 changes: 29 additions & 9 deletions nibiru/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ def __init__(
else grpc.secure_channel(network.grpc_endpoint, credentials)
)
self.insecure = insecure
self.stubCosmosTendermint = tendermint_query_grpc.ServiceStub(self.chain_channel)
self.stubCosmosTendermint = tendermint_query_grpc.ServiceStub(
self.chain_channel
)
self.stubAuth = auth_query_grpc.QueryStub(self.chain_channel)
self.stubAuthz = authz_query_grpc.QueryStub(self.chain_channel)
self.stubBank = bank_query_grpc.QueryStub(self.chain_channel)
Expand Down Expand Up @@ -74,7 +76,9 @@ def get_latest_block(self) -> tendermint_query.GetLatestBlockResponse:

def get_account(self, address: str) -> Optional[auth_type.BaseAccount]:
try:
account_any = self.stubAuth.Account(auth_query.QueryAccountRequest(address=address)).account
account_any = self.stubAuth.Account(
auth_query.QueryAccountRequest(address=address)
).account
account = auth_type.BaseAccount()
if account_any.Is(account.DESCRIPTOR):
account_any.Unpack(account)
Expand All @@ -86,7 +90,11 @@ def get_request_id_by_tx_hash(self, tx_hash: bytes) -> List[int]:
tx = self.stubTx.GetTx(tx_service.GetTxRequest(hash=tx_hash))
request_ids = []
for tx in tx.tx_response.logs:
request_event = [event for event in tx.events if event.type == "request" or event.type == "report"]
request_event = [
event
for event in tx.events
if event.type == "request" or event.type == "report"
]
if len(request_event) == 1:
attrs = request_event[0].attributes
attr_id = [attr for attr in attrs if attr.key == "id"]
Expand All @@ -97,25 +105,33 @@ def get_request_id_by_tx_hash(self, tx_hash: bytes) -> List[int]:
raise NotFoundError("Request Id is not found")
return request_ids

def simulate_tx(self, tx_byte: bytes) -> Tuple[Union[abci_type.SimulationResponse, grpc.RpcError], bool]:
def simulate_tx(
self, tx_byte: bytes
) -> Tuple[Union[abci_type.SimulationResponse, grpc.RpcError], bool]:
try:
req = tx_service.SimulateRequest(tx_bytes=tx_byte)
return self.stubTx.Simulate.__call__(req), True
except grpc.RpcError as err:
return json.loads(err._state.debug_error_string)["grpc_message"], False

def send_tx_sync_mode(self, tx_byte: bytes) -> abci_type.TxResponse:
req = tx_service.BroadcastTxRequest(tx_bytes=tx_byte, mode=tx_service.BroadcastMode.BROADCAST_MODE_SYNC)
req = tx_service.BroadcastTxRequest(
tx_bytes=tx_byte, mode=tx_service.BroadcastMode.BROADCAST_MODE_SYNC
)
result = self.stubTx.BroadcastTx.__call__(req)
return result.tx_response

def send_tx_async_mode(self, tx_byte: bytes) -> abci_type.TxResponse:
req = tx_service.BroadcastTxRequest(tx_bytes=tx_byte, mode=tx_service.BroadcastMode.BROADCAST_MODE_ASYNC)
req = tx_service.BroadcastTxRequest(
tx_bytes=tx_byte, mode=tx_service.BroadcastMode.BROADCAST_MODE_ASYNC
)
result = self.stubTx.BroadcastTx.__call__(req)
return result.tx_response

def send_tx_block_mode(self, tx_byte: bytes) -> abci_type.TxResponse:
req = tx_service.BroadcastTxRequest(tx_bytes=tx_byte, mode=tx_service.BroadcastMode.BROADCAST_MODE_BLOCK)
req = tx_service.BroadcastTxRequest(
tx_bytes=tx_byte, mode=tx_service.BroadcastMode.BROADCAST_MODE_BLOCK
)
result = self.stubTx.BroadcastTx.__call__(req)
return result.tx_response

Expand All @@ -133,7 +149,11 @@ def get_grants(self, granter: str, grantee: str, **kwargs):
)

def get_bank_balances(self, address: str):
return self.stubBank.AllBalances(bank_query.QueryAllBalancesRequest(address=address))
return self.stubBank.AllBalances(
bank_query.QueryAllBalancesRequest(address=address)
)

def get_bank_balance(self, address: str, denom: str):
return self.stubBank.Balance(bank_query.QueryBalanceRequest(address=address, denom=denom))
return self.stubBank.Balance(
bank_query.QueryBalanceRequest(address=address, denom=denom)
)
Loading